Gag*_*iOS 1 navigation uinavigationbar uinavigationcontroller ios swift
在我的 iPhone 应用程序中,我有两个 NavigationC0nroller 背景颜色和标题颜色
我正在使用如下通用代码来更改背景颜色和标题颜色
@objc class Helper : NSObject{
class func restNavigationBarWithNavigationController(navigationController : UINavigationController , withColor : UIColor,isTranslucent : Bool )
{
navigationController.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController.navigationBar.shadowImage = UIImage()
navigationController.navigationBar.isTranslucent = isTranslucent
navigationController.view.backgroundColor = withColor
navigationController.navigationBar.backgroundColor = withColor
}
}
Run Code Online (Sandbox Code Playgroud)
我在视图控制器的 viewdidload 和 ViewWillAppear 中调用上面,如下所示
self.navigationController?.navigationBar.tintColor = .white
Helper.restNavigationBarWithNavigationController(navigationController:
self.navigationController!, withColor: .clear, isTranslucent: true)
Run Code Online (Sandbox Code Playgroud)
但是上面的代码不起作用如果我从白色弹出一个屏幕到红色标题&反之亦然。
请让我知道我在这里做错了什么。
任何想法或建议都会很棒。(仅供参考:我在 Objective-c 视图控制器中使用相同的代码库)
小智 6
好吧,如果您想更改每个屏幕中的导航栏样式,可以使用以下代码:
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// White color Background + Red Color Title
navigationController.navigationBar.barTintColor = .white
navigationController.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.red]
}
}
Run Code Online (Sandbox Code Playgroud)
// 下一个
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Clear color Background + White color title
navigationController.navigationBar.barTintColor = .clear
navigationController.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
}
}
Run Code Online (Sandbox Code Playgroud)
这适用于 Swift 4
| 归档时间: |
|
| 查看次数: |
5720 次 |
| 最近记录: |