swift微博第7天(导航条按钮的封装)

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.带图片的导航条内容 (一)

带图片的导航条内容
// 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 (二)

带图片的导航条内容: 利用button
 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微博第7天(导航条按钮的封装))