Swift Navigation bar background color white does n

2019-01-20 18:37发布

Im having a difficult time trying to change the navigationBar.barTintColor to white. Strangely, all other colors work, but not white!

self.navigationController!.navigationBar.barTintColor = UIColor.whiteColor()

Above line doesnt work just for white.

Even tried with a background image. Still the same. Any other color works but not white!! White is always replaced by light grey...

Please advice where I am going wrong...

Thanks.

1条回答
狗以群分
2楼-- · 2019-01-20 19:17

Try this Code:

In your viewDidLoad:

    title = "Some Title"

    UIApplication.shared.statusBarStyle = .default
    navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
    navigationController?.navigationBar.shadowImage = UIImage()
    navigationController?.navigationBar.isTranslucent = true

    //Line under NavigationBar
    let barView = UIView(frame: CGRect(x:0, y:(UINavigationController().navigationBar.frame.height + UIApplication.shared.statusBarFrame.height), width:view.frame.width, height:0.6))
    barView.backgroundColor=UIColor.red // set any colour you want..
    navigationController?.navigationBar.addSubview(barView)

    //Title Colour
     navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.red]

Note:

Above code works on any background colour.... If you want the navigationBar to be green....set your view background colour to green...

enter image description here

查看更多
登录 后发表回答