I am going create a scrollView and getting the velocity for other actions
the code in UIKit scrollView is
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)
But SwiftUI Drag Gesture seems cannot get the value when I drag the scrollView It needs to be waiting at the end of the scrolling animation end.
Plz
I have tried
.gesture(
DragGesture().onChange ..
)
But it does not work me
Related
I have a page which has a dark blue background color. When a button is pressed, a sheet is opened, however behind the sheet the blue background shifts down and it shows a white background. How can I prevent this?
ZStack {
setBackgroundColor.darkBlue
.ignoresSafeArea(.all)
HStack {
Button(action: {
self.showSheet = true
}) {
Text("Add exercise")
.frame(maxWidth: .infinity)
.frame(height: 10)
.foregroundColor(CustomColor.darkBlue)
.padding()
.background(CustomColor.cyan)
}
.clipShape(Capsule())
.sheet(isPresented: $showSheet) {
AddWorkoutSheet()
}
}
.padding(.bottom)
}
Blue background shifted when sheet is opened.
I have tried to find out what is wrong, but I cannot seem to figure it out. I quite new to programming and SwiftUI.
It is not shifting down, it is just how sheet works. When you toggle a sheet, it will pop out from the bottom with a default white background, therefore covering the background of your root view. So what you want might be a transparent sheet. If this is the case, using sheet may not be the best way as implementing a transparent sheet needs some walkaround. You may instead try using a transition to move the view up or down with offset.
Started learning SwiftUI, I want to remove this highlighted space between navigation title and Text inside VStack.
If you were to remove the NavigationView and had your VStack as the top-level view in your body, you'd see much the same layout.
That's because its frame is smaller than the frame available for it (in this case, most of the screen) and by default the smaller internal view gets centered both horizontally and vertically.
To move the text to the top of the screen, you need to ensure that your view will grow in size. The easiest way will be add to add a Spacer to the bottom of the VStack:
VStack(alignment: ...) {
Text("...")
Text("...")
Spacer()
}
Another approach would be to wrap your VStack in a scroll view. This view will automatically expand itself to fill the height, and layout its subviews (your VStack) from the top downwards:
ScrollView {
VStack(alignment: ...) {
Text("...")
Text("...")
}
}
Each approach has upsides and downsides depending on how much other data you expect to also be in the view.
You could also manually adjust the frame of your VStack using the .frame() modifier:
VStack(alignment: ...) {
Text("...")
Text("...")
}
.frame(maxHeight: .infinity, alignment: .top)
This would give you much the same effect as using the spacer. But the spacer version is a better way to go, especially if you're only starting out with SwiftUI layout.
When I embed my view's body inside of a NavigationView I lose all ability to select subviews within the preview window, as well as the reverse ability of selecting code to trigger and display the blue border around the associated subview in the preview. It only shows a blue border around the entire NavigationView no matter what or where I click. Is this a bug? If not, what am I doing wrong?
Edit: Per the request of the comments below, here is a simple example:
struct FooView: View {
var body: some View {
NavigationView {
VStack {
Label("Try")
Label("Selecting")
Label("These")
}
}
}
}
I would like the NavigationBar (from a SwiftUI NavigationView) to just stay large and scroll out whenever I scroll in a Pages ScrollView. Right now it is always collapsing and showing even on scroll in ".inline"-display mode. So when I try to completely hide it I also hide the large title I'd like to keep.
Is there a way to just let it stay large and just scroll out?
Any solution in SwiftUI or UIKit is appreciated as I can introspect from UIKit.
I especially don't want the NavigationBar to be hidden and rebuild by VStack or HStack as this causes loosing all Navigation Gestures like swiping back to the previous Navigation View and so...
Here is a basic codesnipped to recreate
struct TestTestView: View {
var body: some View {
NavigationView {
ScrollView() {
Color.green
.frame(height: 1000)
}
.navigationTitle("Test")
}
}
}
I'm trying to make navigation view that leads to destination view with scroll view, where navigation title of destination view would animate towards inline display mode or at least scroll behind the nav bar itself.
Basically I'm trying to replicate behavior of standard Music app, specifically when you go from Library to Songs.
There you have source view (Library) with its own title that is animated into inline display mode on scroll. When you tap Songs you also get list with new title (Songs) that also animates into inline display mode on scroll.
So I have main NavigationView with NavigationBarTitle. I move to destinationView with its own NavigationBarTitle and some long list of content. On scroll, NavigationBarTitle of main Navigation view changes to inline display mode, but NavigationBar of destination view behaves very odd: it's basically an overlay with no background and no animation.
And if you remove NavigationBarTitle of destination view all together it only makes things worse. It seems like it adds another transparent NavigationBar with nothing in it.
Also tried to add background to the navigation bar, looked around documentation, but found no solution.
Not sure if I'm doing something very wrong or it's just beta bug of SwiftUI or Xcode.
import UIKit
struct ContentView: View {
var body: some View {
NavigationView{
List(0..<20) { item in
NavigationLink(destination: DetailedView()) {
Text("Next view")
}
}
.navigationBarTitle("Source View")
}
}
}
struct DetailedView: View {
var body: some View {
List(0...25) { number in
Text("This is \(number)'th row")
}
.navigationBarTitle(Text("Destination View"))
// comment out line above to see empty frame of navigation bar
}
}
This is not a full answer to your question, but a temporary workaround: add top or vertical padding to your list on the child view, depending on your preference. This is what I have been doing until there is a better solution.
This will at least make the content scroll under the navigation header with a proper background rendered behind the header. It doesn't have the nice animation to make the title smaller.
struct DetailedView: View {
var body: some View {
List(0...25) { number in
Text("This is \(number)'th row")
}
.padding(.top)
.navigationBarTitle(Text("Destination View"))
}
}
This is fixed in the iOS 13.1 public release (with the App Store release of Xcode 11).
I'm currently on beta 5, and I think this is an ongoing bug with SwiftUI.
I noticed the same issue while doing the SwiftUI Landmarks Tutorials, and you can easily reproduce the issue: https://imgur.com/a/aYgUUH0
For now, to avoid seeing all the content scroll under a transparent navBar, I've converted all of my Navbars to display as inline, since automatic and large experience the issue.
List {
// ...
}
.navigationBarTitle(Text("MyTitle"), displayMode: .inline)