Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

swiftui - Swift: Control Navigation Bar displayMode

I'm trying to figure out how to programatically control the default behaviour that happens when you scroll up on a Navigation View's Child View.

On scroll the Navigation Bar's black title (.large) is hidden by the Navigation Bar's .inline title that slowly fades in. GIF attached.

enter image description here

However I already have a title for my page (the white text) that appears further down. Is there a way to hide the default black title, and control when that .inline title fades in. I ask because I want to replicate that Navigation Bar scroll effect, except I only want that .inline title to begin fading in once the white title goes off screen. I've attempted to illustrate this in the below image.

enter image description here

I think I have to dip into UIKit for this level of customisability so I've begun screwing around with UINavigationBar.appearance() to hide the .large Navigation Bar title. But I'm not sure how to control when and how the Navigation Bar appears. I assume I have to use the scroll position to trigger that animation.

I'm pretty new to Swift so help is appreciated. Thanks in advance!

question from:https://stackoverflow.com/questions/65928402/swift-control-navigation-bar-displaymode

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The only thing to do in this case is to use the custom title view in the navigation bar, and not use the default navigation title at all.

NavigationView { 
    YourView()
        .navigationBarTitleDisplayMode(.inline)
        .toolbar {
            ToolbarItem(placement: .principal) {
                Text("Title").opacity(titleOpacity)
            }
        }
}

You will need to calculate the value of titleOpacity yourself by observing the offset value of the scroll view, and adjusting the parameters accordingly.

@State private var scrollOffset: CGFloat = 0

private var titleOpacity: Double {
    // calculate the opacity based of current scroll view offset
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...