iOS 13 & Xcode 11 - UINavigationBar 大标题滚动中断
iOS 13 & Xcode 11 - UINavigationBar broken scrolling for large titles
在 iOS 12 和 Xcode 10.3 上,大标题滚动没有问题,但使用相同的代码,Xcode 11 和 iOS 13,我遇到如下GIF所示问题:
在 iOS 12 时,我的导航栏具有所需的行为,即:
有人遇到同样的问题吗?对于其余部分,我当然使用 prefersLargeTitles = true
并且我确定我使用的代码对于这两种不同的行为是相同的。感谢您的帮助
经过一番调查,我找到了解决方案,所以我会分享我的发现,因为我认为可以帮助一些人。
解决方案是,从 iOS 13,我们必须使用 UINavigationBarAppearance
。一旦我们创建了这个对象,我们就可以将它分配给一些名为的新属性:
standardAppearance
compactAppearance
scrollEdgeAppearance
(这个特别可能是我的错误的原因)
我post一个它作为扩展的例子:
extension UINavigationBar {
func setupLarge() {
// ... Set up here your tintColor, isTranslucent and other properties if you need
if #available(iOS 11.0, *) {
prefersLargeTitles = true
//largeTitleTextAttributes = ...Set your attributes
}
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = barTintColor
appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
standardAppearance = appearance
compactAppearance = appearance
scrollEdgeAppearance = appearance
}
}
}
在 iOS 12 和 Xcode 10.3 上,大标题滚动没有问题,但使用相同的代码,Xcode 11 和 iOS 13,我遇到如下GIF所示问题:
在 iOS 12 时,我的导航栏具有所需的行为,即:
有人遇到同样的问题吗?对于其余部分,我当然使用 prefersLargeTitles = true
并且我确定我使用的代码对于这两种不同的行为是相同的。感谢您的帮助
经过一番调查,我找到了解决方案,所以我会分享我的发现,因为我认为可以帮助一些人。
解决方案是,从 iOS 13,我们必须使用 UINavigationBarAppearance
。一旦我们创建了这个对象,我们就可以将它分配给一些名为的新属性:
standardAppearance
compactAppearance
scrollEdgeAppearance
(这个特别可能是我的错误的原因)
我post一个它作为扩展的例子:
extension UINavigationBar {
func setupLarge() {
// ... Set up here your tintColor, isTranslucent and other properties if you need
if #available(iOS 11.0, *) {
prefersLargeTitles = true
//largeTitleTextAttributes = ...Set your attributes
}
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = barTintColor
appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
standardAppearance = appearance
compactAppearance = appearance
scrollEdgeAppearance = appearance
}
}
}