问题
We are currently having an issue with navigation bar sizing when using modal presentation in iOS 13.
In most cases this works fine as can be seen in this screenshot:
However, in a few screens we get this weird effect, with the navigation bar having a lower height and a weird "see-through" gap between it and the view. As seen in this screenshot:
Both of the view controllers have the same values set for their properties, are modally presented and have the same constrains on their subviews (0 spacing from the superview/margins/top layout guide).
This issue doesn't happen in iOS 12, even when built with the iOS 13 SDK. Is this a known issue in iOS 13 (beta 8), or is there something we should adjust in the code/storyboard?
回答1:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if #available(iOS 13.0, *) {
navigationController?.navigationBar.setNeedsLayout()
}
}
We found this work around here and it worked for us.
回答2:
Like Rod's answer, but I found it only works if I put setNeetsLayout() in next main thread runLoop:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Workaround for iOS 13 modal gap below navigationbar
if #available(iOS 13.0, *) {
DispatchQueue.main.async {
self.navigationController?.navigationBar.setNeedsLayout()
}
}
}
来源:https://stackoverflow.com/questions/57784596/how-to-prevent-gap-between-uinavigationbar-and-view-in-ios-13