swift导航栏的设置

简介: swift导航栏的设置

1. 利用系统的方法来创建UIBarButtonItem

//navigationController?.navigationBar.tintColor = UIColor.orange
    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.plain, target: self,action: #selector(BaseViewController.registerWillClick))
    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.plain, target: self,action: #selector(BaseViewController.logionWillClick))

2. 设置导航条和工具条的外观,因为外观设定全局有效,所以应该在程序一进来就设定,在AppDelegate里面设施

UINavigationBar.appearance().tintColor = UIColor.orange
    UITabBar.appearance().tintColor = UIColor.orange

3.带图片的导航条内容 (一)


image.png

// MARK: 初始化导航条内容
private func setUpNav(){
    navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named:"navigationbar_friendattention"),style:UIBarButtonItemStyle.plain,target:self,action:#selector(HomeViewController.KK))
}
@objc private func KK(){
   print("点击了左边")
}


4.带图片的导航条内容: 利用button (二)


image.png

let leftBtn = UIButton()
 leftBtn.setImage(UIImage(named: "navigationbar_friendattention"), for: UIControlState.normal)
 leftBtn.setImage(UIImage(named: "navigationbar_friendattention_highlighted"), for: UIControlState.highlighted)
 leftBtn.addTarget(self, action: #selector(HomeViewController.leftAction), for: UIControlEvents.touchUpInside)
 // button自适应大小
 leftBtn.sizeToFit()
 navigationItem.leftBarButtonItem = UIBarButtonItem(customView:leftBtn)
 // 2.定义右边的按钮
 let rightBtn = UIButton()
 rightBtn.setImage(UIImage(named: "navigationbar_pop"), for: UIControlState.normal)
 rightBtn.setImage(UIImage(named: "navigationbar_pop_highlighted"), for: UIControlState.highlighted)
 // button自适应大小
 rightBtn.sizeToFit()
 rightBtn.addTarget(self, action: #selector(HomeViewController.rightAction), for: UIControlEvents.touchUpInside)
 navigationItem.rightBarButtonItem = UIBarButtonItem(customView:rightBtn)
@objc private func leftAction(){
   print("点击了左边")
}
@objc private func rightAction(){
    print("点击了右边")
}

5. UIBarButtonItem 在控制器里面的封装

1.封装
// MARK: 导航栏方法的封装
private func create(name: String,target: Any?, action: Selector) -> UIBarButtonItem{
    let rightBtn = UIButton()
    rightBtn.setImage(UIImage(named: name), for: UIControlState.normal)
    rightBtn.setImage(UIImage(named: name + "_highlighted"), for: UIControlState.highlighted)
    // button自适应大小
    rightBtn.sizeToFit()
    rightBtn.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
    return UIBarButtonItem(customView:rightBtn)
}
2.方法的实现
 @objc private func leftActi1on(){
    print("点击了左边")
 }
 3.调用
  navigationItem.leftBarButtonItem = create(name: "navigationbar_friendattention", target: self, action: #selector(HomeViewController.leftActi1on))

6. UIBarButtonItem 类目的封装

// 1.类目的创建
 import UIKit
 extension UIBarButtonItem{
 class func createBarbuttonItem(name: String,target: Any?, action: Selector) -> UIBarButtonItem{
    let rightBtn = UIButton()
    rightBtn.setImage(UIImage(named: name), for: UIControlState.normal)
    rightBtn.setImage(UIImage(named: name + "_highlighted"), for: UIControlState.highlighted)
    // button自适应大小
    rightBtn.sizeToFit()
    rightBtn.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
    return UIBarButtonItem(customView:rightBtn)
    }
 }
 // 2. 调用类的封装来创建 UIBarButtonItem
 navigationItem.leftBarButtonItem = UIBarButtonItem.createBarbuttonItem(name: "navigationbar_friendattention", target: self, action: #selector(HomeViewController.leftAction))
 // 3.方法的实现
 @objc private func leftAction(){
    print("点击了左边")
 }


目录
相关文章
|
Swift
Swift 设置阴影
阴影的设置是通过layer的shadow设置的,其中offset为阴影的偏移量,探索下offset是如何影响阴影显示的
801 0
Swift 设置阴影
|
Swift
Swift之设置UItextField的占位文字颜色颜色
Swift之设置UItextField的占位文字颜色颜色
268 0
Swift之设置UItextField的占位文字颜色颜色
|
Swift
swift协议的设置和实现
swift协议的设置和实现
258 0
swift4.0 代码设置阴影
有的时候,项目中会用到阴影的效果,增加立体感。我们可以让设计师给出切图,用图片填充。我们还可以使用几句代码来实现简单的阴影效果。 实现效果图展示一: image.
1494 0
swift 为UIView设置投影效果
//视频标题投影 videoBackBtn.layer.shadowOffset = CGSizeMake(3,3) videoBackBtn.
1029 0
|
Swift
swift UI专项训练22 设置网址和载入指示器
  上一话中我们对网络浏览器的UI做了初步的设置,那么这一话中,我们来设置网址和载入指示器。
773 0
|
Swift
swift UI专项训练23 设置委托和键盘返回
      前几话中我们搞定了布局,这一话我们首先要把引用全部加上,就是连线到控制器中 UIViewController { @IBOutlet weak var textField1: UITextField! @IBOutlet weak var web1: UIWebView! 然后就是代理,首先来看一下我们手机场景界面上的三个按钮: 第一个按钮是控制器本身,第二个按钮是这个界面的第一反应者,我们这个界面的第一反应者是网址这个框,第三个按钮是退出。
769 0