Swift自定义UINavigationController(背景颜色、背景图片、返回按钮设置、字体大小等)


1.0  自定义UINavigationController时,背景图片、颜色等只需要设置一次,所以我们可以重写  initializa  这个方法来实现我们想要的效果 

 

    override  class  func initialze(){

  let navBar = UINavigationBar.appearence()

  navBar.barTintColor = UIColor.redColor()  // 设置导航栏背景颜色  为红色  

  var attrs = [String : AnyObject]()

  attrs[NSFontAttributeName] = UIFont.systemFontOfSize(15)  

  

  attrs[NSForegroundColorAttributeName] = UIColor.whiteColor()

  

  navBar.titleTextAttributes = attrs  // 设置导航栏标题颜色 字体大小

 

  // 设置导航栏背景图片  

   navBar.setBackgroundImage(UIImage(named: "tabbar_home"), forBarMetrics: .Default)

    //navBar.setBackgroundImage(UIImage(named: "tabbar_discover"), forBarPosition: .Any, barMetrics: .Default)

  

   

    let navItem = UIBarButtonItem.appearance()  // 获取全局的BarButtonItem  

    

    navBar.backItem?.leftBarButtonItem?.customView?.backgroundColor = UIColor.whiteColor()

    

    navBar.tintColor = UIColor.whiteColor()  // 全局设置 BarButtonItem 的颜色  为白色

    

    

    navItem.setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), forBarMetrics: .Default)   // 通过设置把系统的自带的返回字切掉,保留返回的指示图标

 

 

 

  注:单一设置导航栏的背景颜色时,控制器view的原点不会向下移动 64 ,当设置导航栏的背景图片时,控制器的view的原点从导航栏下方开始,也就是向下移动 64 

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM