为什么我的导航栏颜色在 Swift 中没有改变?
Mhy my NavigationBar Colour is not changing in Swift?
我自定义了 NavigationBar
以在所有屏幕上使用,如下所示:
import Foundation
import UIKit
extension UIViewController {
func setUpNavigationBar() {
self.navigationController?.navigationBar.backgroundColor = .yellow
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.layer.masksToBounds = false
let menuImageView = UIImageView.init(image: #imageLiteral(resourceName: "menu8"))
menuImageView.tintColor = .darkGray
menuImageView.frame = CGRect(x:0.0,y:0.0, width:25,height:25.0)
menuImageView.contentMode = .scaleAspectFit
menuImageView.widthAnchor.constraint(equalToConstant: 25).isActive = true
menuImageView.heightAnchor.constraint(equalToConstant: 25).isActive = true
menuImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(menuClick)))
let btnMenu = UIBarButtonItem(customView: menuImageView)
self.navigationItem.setLeftBarButton(btnMenu, animated: true)
}
@objc func menuClick(){
SideMenuController.preferences.basic.menuWidth = self.view.frame.width*0.8
if !(self.sideMenuController?.isMenuRevealed ?? false){
self.sideMenuController?.revealMenu()
}
}
}
并像这样调用 SignupVC ViewWillAppear
:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
setUpNavigationBar()
}
使用上面的代码,我没有得到黄色 NavigationBar
。我在 SignupVC 中得到 NavigationBar
就像唯一的白色。没有 leftbutton
菜单。为什么?我哪里错了?
谢谢。
更改背景:
navigationController?.navigationBar.barTintColor = UIColor.yellow
测试正常 100%
此代码:
func setUpNavigationBar() {
navigationController?.navigationBar.barTintColor = UIColor.yellow
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.layer.masksToBounds = false
let menuImageView = UIImageView.init(image: UIImage.init(named: "day")!)
menuImageView.tintColor = .darkGray
menuImageView.frame = CGRect(x:0.0,y:0.0, width:25,height:25.0)
menuImageView.contentMode = .scaleAspectFit
menuImageView.widthAnchor.constraint(equalToConstant: 25).isActive = true
menuImageView.heightAnchor.constraint(equalToConstant: 25).isActive = true
menuImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(menuClick)))
let btnMenu = UIBarButtonItem(customView: menuImageView)
self.navigationItem.setLeftBarButton(btnMenu, animated: true)
}
我自定义了 NavigationBar
以在所有屏幕上使用,如下所示:
import Foundation
import UIKit
extension UIViewController {
func setUpNavigationBar() {
self.navigationController?.navigationBar.backgroundColor = .yellow
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.layer.masksToBounds = false
let menuImageView = UIImageView.init(image: #imageLiteral(resourceName: "menu8"))
menuImageView.tintColor = .darkGray
menuImageView.frame = CGRect(x:0.0,y:0.0, width:25,height:25.0)
menuImageView.contentMode = .scaleAspectFit
menuImageView.widthAnchor.constraint(equalToConstant: 25).isActive = true
menuImageView.heightAnchor.constraint(equalToConstant: 25).isActive = true
menuImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(menuClick)))
let btnMenu = UIBarButtonItem(customView: menuImageView)
self.navigationItem.setLeftBarButton(btnMenu, animated: true)
}
@objc func menuClick(){
SideMenuController.preferences.basic.menuWidth = self.view.frame.width*0.8
if !(self.sideMenuController?.isMenuRevealed ?? false){
self.sideMenuController?.revealMenu()
}
}
}
并像这样调用 SignupVC ViewWillAppear
:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
setUpNavigationBar()
}
使用上面的代码,我没有得到黄色 NavigationBar
。我在 SignupVC 中得到 NavigationBar
就像唯一的白色。没有 leftbutton
菜单。为什么?我哪里错了?
谢谢。
更改背景:
navigationController?.navigationBar.barTintColor = UIColor.yellow
测试正常 100%
此代码:
func setUpNavigationBar() {
navigationController?.navigationBar.barTintColor = UIColor.yellow
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.layer.masksToBounds = false
let menuImageView = UIImageView.init(image: UIImage.init(named: "day")!)
menuImageView.tintColor = .darkGray
menuImageView.frame = CGRect(x:0.0,y:0.0, width:25,height:25.0)
menuImageView.contentMode = .scaleAspectFit
menuImageView.widthAnchor.constraint(equalToConstant: 25).isActive = true
menuImageView.heightAnchor.constraint(equalToConstant: 25).isActive = true
menuImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(menuClick)))
let btnMenu = UIBarButtonItem(customView: menuImageView)
self.navigationItem.setLeftBarButton(btnMenu, animated: true)
}