Swift修改NavigationBar title 字体的颜色

当我们懒加载时:

// swift代码
lazy var navBar:UINavigationBar={
  let bar=UINavigationBar()
  bar.titleTextAttributes=[NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName:UIFont.systemFont(ofSize:19)]
  bar.tintColor=UIColor.white
  return bar
}()

这样写的是可以改变title字体颜色的

但是从xib拖到controller时:

// swift代码
@IBOutletweak var navigationBar:UINavigationBar!{
   didSet{
    navigationBar.titleTextAttributes= [NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName:UIFont.systemFont(ofSize:19)]
    navigationBar.tintColor=UIColor.white
  }
}

这样设置没有效果

正确姿势:

// swift代码
navigationBar.titleTextAttributes=[NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName:UIFont.systemFont(ofSize:19)]
navigationBar.tintColor=UIColor.white

将设置放到viewDidLoad()中

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容