Last active
July 24, 2022 09:38
-
-
Save aaronlab/171c77eb21864d6ba726de98fb845909 to your computer and use it in GitHub Desktop.
SwiftUI Navigation Link Issue Solution: Navigation Bar overlapped
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct ContentView: View { | |
var body: some View { | |
NavigationView { | |
VStack { | |
NavigationLink(destination: ChildView()) { Text("Child View") } | |
.isDetailLink(false) | |
/* | |
If you don't add .isDetailLink(false) above, | |
the title of the navigation bar in the child view with ".navigationBarBackButtonHidden(true)" will be overlapped | |
on the navigation bar of the parent view, | |
especially when you decide not to pop the child view and then make it back on the top. | |
*/ | |
Text("Parent View") | |
} //: V | |
} //: N | |
} | |
} | |
struct ChildView: View { | |
var body: some View { | |
Text("Child View") | |
.navigationBarBackButtonHidden(true) | |
} | |
} | |
/* | |
If you don't add the extension below, | |
the back(pop) gesture in the child view of a parent view will never work, | |
when you make the back button hidden. | |
*/ | |
extension UINavigationController: UIGestureRecognizerDelegate { | |
override open func viewDidLoad() { | |
super.viewDidLoad() | |
interactivePopGestureRecognizer?.delegate = self | |
} | |
public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool { | |
return viewControllers.count > 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment