And*_*nez 26 uinavigationbar uikit ios ios11
我有一个要求,我必须使用UINavigationBar红色大标题.
目前,我有以下代码:
func prepareNavigationController() {
let navController = UINavigationController(rootViewController: self)
navController.navigationBar.prefersLargeTitles = true
navigationItem.searchController = UISearchController(searchResultsController: nil)
navigationItem.hidesSearchBarWhenScrolling = false
navController.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.red]
}
Run Code Online (Sandbox Code Playgroud)
但它实际上并没有将标题标签染成红色.这是结果:
但prefersLargeTitles改为假是正确的,我的头衔是红色的.
navController.navigationBar.prefersLargeTitles = false
我不完全确定这是否是一个错误,因为在撰写本文时我们仍处于第一个测试阶段,或者如果这是故意行为,主要是因为我之前没有任何Apple的应用程序为大型游戏着色.有没有办法真正让大标题拥有我想要的任何颜色?
ass*_*0yr 54
有一个新的UINavigationBar属性"largeTitleTextAttribute"应该有助于此.
下面是一个示例代码,您可以将其添加到视图控制器viewDidLoad方法中
navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue]
Run Code Online (Sandbox Code Playgroud)
以下是没有设置largeTitleTextAttributes的示例代码和屏幕截图,但barStyle设置为.black
navigationController?.navigationBar.barStyle = .black
Run Code Online (Sandbox Code Playgroud)
这是没有设置largeTitleTextAttributes的屏幕截图,但barStyle设置为.default
navigationController?.navigationBar.barStyle = .default
Run Code Online (Sandbox Code Playgroud)
不确定它是否是测试版1和2中的错误,但这是一种设置颜色的方法.这是一个"hacky"解决方法,但它应该工作,直到Apple修复此问题.在Objective-C和Swift版本中,此代码都在该viewDidAppear:方法中.
Objective-C的:
dispatch_async(dispatch_get_main_queue(), ^{
for (UIView *view in self.navigationController.navigationBar.subviews) {
NSArray <__kindof UIView *> *subviews = view.subviews;
if (subviews.count > 0) {
UILabel *label = subviews[0];
if (label.class == [UILabel class]) {
[label setTextColor:[UIColor redColor]];
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
迅速:
DispatchQueue.main.async {
for view in self.navigationController?.navigationBar.subviews ?? [] {
let subviews = view.subviews
if subviews.count > 0, let label = subviews[0] as? UILabel {
label.textColor = UIColor.red
} } }
Run Code Online (Sandbox Code Playgroud)
您在iOS 13中执行此操作的方式已更改,现在您可以使用UINavigationBarAppearance此类...
let appearance = UINavigationBarAppearance(idiom: .phone)
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.systemRed]
appearance.titleTextAttributes = [.foregroundColor: UIColor.systemRed]
appearance.backgroundColor = .white
navigationItem.standardAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9047 次 |
| 最近记录: |