I have a View with a DragGesture to resize something, so I tried using onHover to set the macOS cursor to the right resize icon. It doesn't work 100%, because the drag will reset the cursor. Then I have to move the pointer out of the view and back in, to re-trigger the onHover and get the resize cursor again.
swift
private struct HeaderEdgeDragArea: View {
var drag: some Gesture {
DragGesture(minimumDistance: 0, coordinateSpace: .global)
.onChanged { value in
...
}
.onEnded { _ in
...
}
}
var body: some View {
Color.gray.zIndex(2.0).frame(width: 1.0)
.overlay(
Rectangle().frame(width: 8)
.foregroundColor(.clear)
.contentShape(Rectangle())
.border(Color.red, width: 0.5)
.onHover { hovering in
print("hovering: \(hovering)")
if hovering {
NSCursor.resizeLeftRight.push()
} else {
NSCursor.pop()
}
}
.gesture(drag)
)
}
}
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I know how to add items to the main menu. But what if I want to connect a handler to one that is already there by default? (For example "Select All").
WindowGroup {
ContentView()
}.commands {
CommandGroup(after: CommandGroupPlacement.pasteboard) {
Button("Select All") { selectAll() }
}
That adds a second "Select All" menu item.
If I use CommandGroup(replacing: ...) then it replaces others, not just the "Select All"
These are properties of Product. Both are type VerificationResult<Transaction>? and they seem very similar. What are some example situations where they would be different?
It would be nice if the documentation discussed this.
Here's the entire app below. On iOS 15, it pops up blank. If you dismiss it and click "Share" again, then it looks right. Is this a bug, or is the code wrong?
struct ContentView: View {
@State private var showShare = false
@State private var shareItems: [Any] = []
var body: some View {
VStack {
Text("Test ActivityViewController")
Button("Share") {
share()
}.sheet(isPresented: $showShare) {
print("dismissed")
} content: {
ActivityViewController(activityItems: shareItems)
}
}
}
func share() {
shareItems = ["test"]
showShare = true
}
}
struct ActivityViewController: UIViewControllerRepresentable {
let activityItems: [Any]
func makeUIViewController(context: Context) -> UIActivityViewController {
let c = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
return c
}
func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) {}
}
I tried to replace UIKit's UIPinchGestureRecognizer with SwiftUI's MagnificationGesture, but it doesn't seem possible in my case. I use the location(in: UIView) function from the former, which allows me to zoom in on that point, specifically. It's a better experience when zooming in on an image.
Can I get that info from the MaginificationGesture? In the example code I see only the CGFloat for the amount of magnification.
This minimal code demonstrates a problem I'm having. As you adjust the Slider (high precision numbers), the TextField sets the model, changing the number's precision to its format.
But I only want it to use the 3-digit fraction for display, or if the user edits in the field. It shouldn't be mutating the model just because it's displaying the value, no?
Is it a bug? It doesn't seem right to me.
struct ContentView: View {
@State var number: Double = 0
// wrap binding to log the `set` calls
var textFieldBinding: Binding<Double> {
Binding { number } set: {
print("Setting from TextField: \($0)")
number = $0
}
}
var sliderBinding: Binding<Double> {
Binding { number } set: {
print("Setting from Slider: \($0)")
number = $0
}
}
var body: some View {
VStack {
TextField("Number", value: textFieldBinding, format: .number.precision(.fractionLength(3)))
Slider(value: sliderBinding, in: 0...5.0)
}.frame(maxWidth: 300)
}
}
When you drag the slider, you see stuff like:
Setting from Slider: 1.0073260217905045
Setting from TextField: 1.007
...
I'm working on a drawing and painting program. When I turn my iPad, I detect the screen change and keep the drawing stable, while allowing the toolbars and things to rotate. But SwiftUI creates a disorienting animation of the main canvas anyway. Is there a way to shut that off?
I'm using the Connect app on my iPad. I'm looking at an app that is barely used, and I see two written reviews. But in the App Store there is only one. Any ideas what could cause this?
The review in question is reporting a possible bug, so I do wonder if this means the user retracted/deleted it.
Hard to believe something this old doesn't work, but it appears so. I set the top to 10 and the bottom to 0, but it uses 10 for both.
Am I doing something wrong, or is it a bug?
sv.edgeInsets = .init(top: 10, left: 10, bottom: 0, right: 0)
Screenshot:
Full code example below.
(This is the ViewController.swift file after creating a new Xcode project with "Interface" of "Storyboard". No other changes to the project template.)
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
let sv = NSStackView()
sv.translatesAutoresizingMaskIntoConstraints = false
let v1 = ColoredBlock(.red, width: 20)
let v2 = ColoredBlock(.green, width: 20)
let v3 = ColoredBlock(.blue)
sv.addArrangedSubview(v1)
sv.addArrangedSubview(v2)
sv.addArrangedSubview(v3)
sv.spacing = 0
sv.edgeInsets = .init(top: 10, left: 10, bottom: 0, right: 0)
// sv.setContentHuggingPriority(.required, for: .vertical)
view.addSubview(sv)
pinToFourEdges(view, sv)
}
}
class ColoredBlock: NSView {
let intrinsicHeight: CGFloat
let intrinsicWidth: CGFloat
init(_ color: NSColor, width: CGFloat = NSView.noIntrinsicMetric, height: CGFloat = NSView.noIntrinsicMetric) {
intrinsicWidth = width
intrinsicHeight = height
super.init(frame: .zero)
self.wantsLayer = true
if let layer = self.layer {
layer.backgroundColor = color.cgColor
}
}
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
override var intrinsicContentSize: NSSize {
return NSSize(width: intrinsicWidth, height: intrinsicHeight)
}
}
public func pinToFourEdges(_ view1: NSView, _ view2: NSView) {
view1.topAnchor.constraint(equalTo: view2.topAnchor).isActive = true
view1.bottomAnchor.constraint(equalTo: view2.bottomAnchor).isActive = true
view1.leftAnchor.constraint(equalTo: view2.leftAnchor).isActive = true
view1.rightAnchor.constraint(equalTo: view2.rightAnchor).isActive = true
}
I expected the initializer NLLanguage(rawValue: "...") to return nil if I gave it junk, but it doesn't. It accepts any string. I'm loading values from a database so I'd like to know if I'm getting one that is valid, like "en", "es", etc., as opposed to an error. Is there anyway to check that?
I'm using it to set NLLanguageRecognizer.languageConstraints, so I guess by "valid" I mean all the languages that work with that property.
The struct has a bunch of static constants:
struct NLLanguage {
static let english = ...
static let spanish = ...
...
}
If those all work with NLLanguageRecognizer I guess I can paste them all into a dictionary.
I have an app that plays synthesized speech. If I call the AVSpeechSynthesizer speakUtterance method with a premium voice, then no noise comes out, and the synth never works again. However, if I first use a non-premium voice, it works, and I can then call it with premium voices and they speak too.
I need to get at the files of an app (one that I developed). It's sitting there on the home screen of my iPad, and I can run it. But in the "Devices and Simulators" window in Xcode, in the "Installed Apps" list, it's not there. That list is how I would normally get the "container" so I can see the files.
Why would an app that is obviously on the device not be in the list? Is there another way to get the container/files?
I have a couple apps in one git repository. I'd like to have a Swift package in that repo as well, shared by apps. In other words, I don't want a separate repo for the Swift package. I followed the instructions here:
https://developer.apple.com/documentation/xcode/organizing-your-code-with-local-packages
It seems to work. I can write code like this in my app:
import MyLocalPackage
func foo() {
myLocalPackageFunc()
}
I notice that the package is not listed under Project > MyApp > Package Dependencies. I don't really care, as long as I can reuse code between apps.
But when I try to add this package code to a 2nd app, I'm at a loss. I tried "Add Package Dependencies" and "Add Local", but that creates a different looking setup than the 1st app. The code is browsable in the project navigator. And when I try to build it says "Missing package product 'MyLocalPackage'.
The documentation linked above, which I used for the 1st app, does a "New > Package". I don't want a new package. How can I connect the existing one?
Hi,I had a document-based iOS app working, but want to change it so it saves to a package. Seems like it's better when big chunks of a file may not be changing. In Xcode, under the Target > Info > Exported UTI > Conforms To, I had: "public.data, public.content". If I change that to "com.apple.package", then I can't open my old files to upgrade them. But if I *add* "com.apple.package", then the app opens both kinds as desired. I wonder if having it conform to all three of those types is going to cause other problems.Rob