我正在更新一个在Xcode10上编译的应用程序,在iOS 13上运行得很好。我想做一些更改,所以在Xcode11上重新编译,现在barTintColor有问题。
如果'Large Titles‘设置为'Always’,我的自定义barTintColor就不会被应用--我只会得到默认的灰色。如果'Large Titles‘设置为'Never',我的自定义barTintColor将按预期应用。如果“大标题”设置为“自动”,则当显示大标题时,NavBar将默认为灰色,而当显示小标题时,将显示我的自定义颜色。例如,当我的导航栏下面的标题被向上推时,默认的大标题切换为小标题,并且我的NavBar会改变颜色。正常的行为是它总是我的自定义颜色。
我的ViewController类中的相关代码,最后一行是设置barTintColor的代码:
override func viewDidLoad() {
super.viewDidLoad()
setDelegates()
setTableViewHeightForCollapsingHeaders()
setNavigtionBarItems()
doSplitViewManagement()
}
override func viewWillAppear(_ animated: Bool) {
clearsSelectionOnViewWillAppear = splitViewController!.isCollapsed
super.viewWillAppear(animated)
updateUI()
}
fileprivate func setNavigtionBarItems() {
//set up UI buttons
navigationItem.leftBarButtonItem = editButtonItem
let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(insertNewObject(_:)))
navigationItem.rightBarButtonItem = addButton
navigationController?.navigationBar.barTintColor = UIColor(hex: 0x5da0a2)
}你知道为什么行为改变了吗,以及如何修复它?
发布于 2020-03-22 10:05:11
从iOS 13 https://developer.apple.com/documentation/uikit/uinavigationbarappearance开始有了一个新的API
您要查找的backgroundColor属性位于超类https://developer.apple.com/documentation/uikit/uibarappearance中
这里有一些额外的示例代码,https://developer.apple.com/documentation/uikit/uinavigationcontroller/customizing_your_app_s_navigation_bar
发布于 2020-03-23 04:58:06
glotcha指出,苹果的文档对于解决这个问题至关重要,尽管还有更多的东西。以下是适用于iOS 13的my setNavigationBarItems()的更新版本:
fileprivate func setNavigtionBarItems() {
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithDefaultBackground()
appearance.backgroundColor = myBackgroundColor
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance
//navigationController?.navigationBar.compactAppearance = appearance
} else {
// Fallback on earlier versions
navigationController?.navigationBar.barTintColor = myBackgroundColor
}在我的例子中,一个关键点是我的导航栏(在Autolayout中)设置为“Large Titles”为“Automatic”。这使得有必要包含.scrollEdgeAppearance行,以便在从大到紧凑转换时应用自定义外观。事实证明,.compactAppearance行并不是必需的,因为我对两者使用相同的颜色。如果我想为大型和紧凑的应用程序设置不同的外观,那么包含.compactAppearance的行也会很有用。
发布于 2021-07-17 23:05:20
如果你想在一个单独的地方完成整个应用:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let barBackgroundColor = UIColor(displayP3Red: 47/255, green: 54/255, blue: 64/255, alpha: 1.0)
let appearance = UINavigationBarAppearance()
appearance.configureWithDefaultBackground()
appearance.backgroundColor = barBackgroundColor
appearance.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
return true
}https://stackoverflow.com/questions/60795035
复制相似问题