After adding a combined gesture to a view, a TextField inside the view would no longer respond when I would tap into it to change the text. I discovered this after adding a custom combined gesture - where I used a long press to start things before dragging. (Note: things still worked if just a drag gesture was added. Not sure what is particularly different between these two cases.)
The combined gesture:
let combined = longPressGesture.simultaneously(with: dragGesture)
The gesture was added to the view with:
.gesture(combined)
I got things to work by adding an onTapGesture{} to the TextField. Didn’t have to put anything into the action. Seems like a side effect whose behavior could change in the future. Appreciate any comments on if this makes sense or other ways to handle.
TextField(“Enter Text”, text: $myText)
.textFieldStyle(RoundedBorderTextFieldStyle())
.onTapGesture {}
In case one would have this issue with a drag gesture, you can set the minimumDistance. This would still register the tap on the textfield to edit it.
DragGesture(minimumDistance: 30, coordinateSpace: .global)
Adding a drag gesture in SwiftUI to a View inside a ScrollView blocks the scrolling
Related
Is there a good way to hide the up down arrows in the picker default style. I am using ios 16. It seems that the older version does not have such arrows.
Also, is there a setting to set the picker's background to the same style as the datepicker in the image without manually setting the background and radious?
I have been struggling on this small feature and tried googling for a few hours but no luck. Any idea will be appreciated
The suggested Menu solution works well if you only have a few options. The problem I've experienced with the Menu solution is that if there are very many options the Menu doesn't automatically scroll to the currently selected option the way the Picker does.
The solution I've used is to use ZStack to place an opaque picker on top of a custom view (my "label"). Setting the opacity modifier on the Picker to 0.025 makes it invisible on your device but it will still trigger when you tap it.
This way you get all the native functionality of the Picker (including scrolling to the selected option) and you can make the label look any way you want without having to create your own custom picker.
Here's the code:
ZStack {
// Custom picker label
Text("\(value)")
.font(.title)
.foregroundColor(.blue)
.styleDataEntry(colorScheme: colorScheme) // a custom formatter View extension
// Invisible picker
Picker("", selection: $value) {
ForEach(0 ..< 200) { option in
Text("\(option)").tag(option)
}
}
.pickerStyle(.menu)
.opacity(0.025)
}
There are two solutions I have seen can be used to change the colour of the title bar in SwiftUI:
Making the use of NavigationView
Ignoring the sage area with .ignoresSafeArea()
I have no use for a NavigationView nor I want to use .ignoresSafeArea() since it helps on giving the TabView bottom tab space.
The only requirement to the solution I am looking for is that it cannot be for iOS 16 and above only, since it will not be viable to use in production for at least the next two years.
TL;DR:
I need to change the colour of the title bar (the upper-most part of the app) with features available in versions prior to iOS 16, and without the use of NavigationView and .ignoresSafeArea()
The white stuff in this image:
I have a two-tabbed tab view and in the second tab there are different pages being displayed. There is a navigationview and navigationlink within some of the pages. The navigation bar has a weird grey background that I can’t make go away. Any ideas how to remove that?
So it turns out I had a .padding around the view for the second tab, so everything within that view was padded, which introduced this. Removing that solved the issue.
I want to embed some SwiftUI in my UIKit-based UI, and unfortunately Apple doesn't provide UIHostingView, only UIHostingController. Can I more or less ignore that controller and just use its view, or do I really need to add it as a child view controller as well? What happens if I don't?
The problem is that finding the parent view controller can be difficult in some contexts. UIView itself doesn't know anything about view controllers, so I'd have to come up with my own way of keeping track of which is the "current" view controller. And I'd rather not do that unless it's actually necessary.
So far in my experiments it's working fine without adding UIHostingController as a child. Device rotation is handled appropriately, and SwiftUI's dark mode override (.colorScheme()) even works through the embedding.
With UIHostingController(rootView:) you just pass in a SwiftUI View.
You can treat it as a UIView by doing:
let myView = UIHostingController(rootView: Text("Hello world!")).view
And then add it as a subview for example:
let parent = UIView()
parent.addSubview(myView)
Is there any way to customize the gesture recognizer used by .onDrag() in a SwiftUI View? The developer documentation states "applying the onDrag(_:) modifier adds the appropriate gestures for drag and drop to this view" but is silent, so far as I can see, as to how to alter the behavior of those gestures. Those gestures wait for a longpress before initiating the drag. I would like to reduce that delay to zero.
Why Required
The app currently uses a custom DragGesture and .offset(value) to effect a drag. This strategy requires that the view in which the drag initiates have a greater .zIndex than any view over which an item might be dragged. Since drags can begin in different views, the .zIndex for each view is managed programmatically through ternary operators.
The .onDrag() functionality puts the dragged item on top of all views regardless of .zIndex. This behavior is now required due to implementation of a magnification gesture, which requires that the magnified view have a .zIndex below that of the other views or it will cover them as it expands. If the magnified view is then the source of a drag, the required .zIndex behaviors (high for drag, low for magnification) are incompatible.
I tried using .clipped() on the magnified view, but that prevents the dragged item from appearing outside of that view.
Apple developer support responds that there is no way to customize the gesture recognizer automatically added by SwiftUI to .onDrag()