UINavigationItem基础

UINavigationItem基础

//导航条 -> 属性导航控制器
        //navigationItem -> 让导航控制器管理的视图控制器
        
        //创建window
        self.window = UIWindow.init(frame: UIScreen.mainScreen().bounds)
        self.window?.backgroundColor = UIColor.whiteColor()
        
        let navC = YTNavigationController(rootViewController: OneViewController())
        self.window?.rootViewController = navC
        
        
        //将状态栏变成白色(默认是黑色)
        //1.在info.plist文件中去添加一个键值对 -> View controller-based status bar appearance:NO
        //2.通过应用程序对象改变状态栏的样式
        //a.拿到当前应用程序对象
        let app = UIApplication.sharedApplication()
        //b.设置状态栏的样式
        //LightContent -> 白色
        app.statusBarStyle = .LightContent

   //定制导航条
        //1.设置背景颜色
        self.navigationBar.barTintColor = UIColor.blackColor()
        
        //2.设置填充颜色
        self.navigationBar.tintColor = UIColor.whiteColor()
 func navigationItemSetting() {
        
        //1.定制rightItem
        //a.通过其他视图来创建一个UIBarButtonItem对象
        let btn = UIButton.init(frame: CGRectMake(0, 0, 50, 30))
        btn.setTitle("注册", forState: .Normal)
        btn.setBackgroundImage(UIImage.init(named: "buttonbar_action.png"), forState: .Normal)
        btn.setTitleColor(UIColor.greenColor(), forState: .Normal)
        btn.addTarget(self, action: "btnAction", forControlEvents: .TouchDown)
    
        let item1 = UIBarButtonItem.init(customView: btn)
        
        
        //b.通过一张图片创建一个UIBarButtonItem对象
        //参数1:图片
        //参数2:item的风格
        //参数3:调用方法的对象
        //参数4:当当前item对应的按钮被点击后会调用的方法
        let item2 = UIBarButtonItem.init(image: UIImage.init(named: "itemImage")?.imageWithRenderingMode(.AlwaysOriginal), style: .Plain, target: self, action: "itemAction:")
        
        //c.通过文字去创建一个UIBarButtonItem对象
        let item3 = UIBarButtonItem.init(title: "注册", style: .Plain, target: self, action: "itemAction:")
        
        //d.通过系统样式去创建UIBarButtonItem对象
        //参数1:指定的系统样式
        let item4 = UIBarButtonItem.init(barButtonSystemItem: .Add, target: self, action: "itemAction:")
        

        //2.设置navigationBar右边的显示
        //a.在右边显示一个视图
        self.navigationItem.rightBarButtonItem = item4
        //b.在右边显示显示多个视图
        //self.navigationItem.rightBarButtonItems = [item4,item3]
        
        //3.设置navigationBar左边的显示
        self.navigationItem.leftBarButtonItem = item2
        //self.navigationItem.leftBarButtonItems = [item1, item2]
        
        //4.设置navigationBar中间的显示
        //a.显示纯文字
        self.title = "登录"
        
        //b.通过一个label显示文字
        let label = UILabel.init(frame: CGRectMake(0, 0, 100, 30))
        label.text = "登录"
        label.textColor = UIColor.yellowColor()
        label.textAlignment = .Center
        
        //c.创建中间显示的分段选中器
        let segement = UISegmentedControl.init(items: ["海贼","火影"])
        segement.frame = CGRectMake(0, 0, 100, 40)
        segement.selectedSegmentIndex = 0
        
        
        //设置中间视图
        self.navigationItem.titleView = segement

        //设置背景颜色
        self.view.backgroundColor = UIColor.orangeColor()

        //设置rightItem
        let item = UIBarButtonItem.init(barButtonSystemItem: .Cancel, target: self, action: "itemAction")
        self.navigationItem.rightBarButtonItem = item
        
        //1.替换系统自带的返回按钮 -> 设置leftBarButtonItem属性
        let item2 = UIBarButtonItem.init(barButtonSystemItem: .Done, target: self, action: "itemAction")
        
        //self.navigationItem.leftBarButtonItem = item2
        
        //2.隐藏系统自带的返回按钮
        self.navigationItem.hidesBackButton = true

toolBar

//将状态栏变成白色(默认是黑色)
        //1.在info.plist文件中去添加一个键值对 -> View controller-based status bar appearance:NO
        //2.通过应用程序对象改变状态栏的样式
        //a.拿到当前应用程序对象
        let app = UIApplication.sharedApplication()
        //b.设置状态栏的样式
        //LightContent -> 白色
        app.statusBarStyle = .LightContent

 //1.定制toolBar(高度是44)
        //toolBar属于导航控制器,默认是隐藏的
        //a.让toolBar显示出来
        self.toolbarHidden = false
        
        //b.设置是否有透明度(默认true->有透明度)
        self.toolbar.translucent = false
        
        //c.设置背景颜色
        self.toolbar.barTintColor = UIColor.yellowColor()
        //d.设置填充颜色
        self.toolbar.tintColor = UIColor.redColor()
        

        //2.定制导航条
        //a.设置背景颜色
        self.navigationBar.barTintColor = UIColor.blackColor()
        
        //b.设置填充颜色
        self.navigationBar.tintColor = UIColor.whiteColor()
class OneViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.greenColor()

        //1.定制navigationItem
        self.navigationItemSetting()
        
        //2.定制toolBarItem
        self.toolBarItemSetting()
    }

}

//MARK: - 定制toolBarItem
extension OneViewController{

    func toolBarItemSetting() {
        
        //创建toolBar上显示的按钮对应的item
        let item1 = UIBarButtonItem.init(barButtonSystemItem: .Camera, target: self, action: "toolBarAction")
        let item2 = UIBarButtonItem.init(barButtonSystemItem: .Add, target: self, action: "toolBarAction")
        let item3 = UIBarButtonItem.init(barButtonSystemItem: .Edit, target: self, action: "toolBarAction")
        let item4 = UIBarButtonItem.init(barButtonSystemItem: .Refresh, target: self, action: "toolBarAction")
        
        
        //a.FlexibleSpace(相当于空格,用来设置每个item之间的间距,间距是自动计算的)
        let space1 = UIBarButtonItem.init(barButtonSystemItem: .FlexibleSpace, target: nil, action: "")
        //b.FixedSpace(相当于空格,用来设置每个item之间的间距,间距需要手动设置)
        let space2 = UIBarButtonItem.init(barButtonSystemItem: .FixedSpace, target: nil, action: "")
        //设置间距
        space2.width = 20
        
        
        //将item添加到toolBar上
        self.toolbarItems = [space2,item1,space2,item2,space2,item3,space2,item4,space2]
        
    }
    
    func toolBarAction() {
        
        print("toolBar被点击")
    }
}

//MARK: - 定制navigationItem
extension OneViewController{
    
    func navigationItemSetting() {
        
        //1.定制rightItem
        //a.通过其他视图来创建一个UIBarButtonItem对象
        let btn = UIButton.init(frame: CGRectMake(0, 0, 50, 30))
        btn.setTitle("注册", forState: .Normal)
        btn.setBackgroundImage(UIImage.init(named: "buttonbar_action.png"), forState: .Normal)
        btn.setTitleColor(UIColor.greenColor(), forState: .Normal)
        btn.addTarget(self, action: "btnAction", forControlEvents: .TouchDown)
    
        let item1 = UIBarButtonItem.init(customView: btn)
        
        
        //b.通过一张图片创建一个UIBarButtonItem对象
        //参数1:图片
        //参数2:item的风格
        //参数3:调用方法的对象
        //参数4:当当前item对应的按钮被点击后会调用的方法
        let item2 = UIBarButtonItem.init(image: UIImage.init(named: "itemImage")?.imageWithRenderingMode(.AlwaysOriginal), style: .Plain, target: self, action: "itemAction:")
        
        //c.通过文字去创建一个UIBarButtonItem对象
        let item3 = UIBarButtonItem.init(title: "注册", style: .Plain, target: self, action: "itemAction:")
        
        //d.通过系统样式去创建UIBarButtonItem对象
        //参数1:指定的系统样式
        let item4 = UIBarButtonItem.init(barButtonSystemItem: .Add, target: self, action: "itemAction:")
        

        //2.设置navigationBar右边的显示
        //a.在右边显示一个视图
        self.navigationItem.rightBarButtonItem = item4
        //b.在右边显示显示多个视图
        //self.navigationItem.rightBarButtonItems = [item4,item3]
        
        //3.设置navigationBar左边的显示
        self.navigationItem.leftBarButtonItem = item2
        //self.navigationItem.leftBarButtonItems = [item1, item2]
        
        //4.设置navigationBar中间的显示
        //a.显示纯文字
        self.title = "登录"
        
        //b.通过一个label显示文字
        let label = UILabel.init(frame: CGRectMake(0, 0, 100, 30))
        label.text = "登录"
        label.textColor = UIColor.yellowColor()
        label.textAlignment = .Center
        
        //c.创建中间显示的分段选中器
        let segement = UISegmentedControl.init(items: ["海贼","火影"])
        segement.frame = CGRectMake(0, 0, 100, 40)
        segement.selectedSegmentIndex = 0
        
        
        //设置中间视图
        self.navigationItem.titleView = segement
        
        
        
    }
    
    func itemAction(item:UIBarButtonItem) {
        
        print("item被点击")
        //跳转到下一个界面
        let two = TwoViewController()
        self.navigationController?.pushViewController(two, animated: true)
        
    }
    
    //按钮点击
    func btnAction() {
        
        print("跳转到注册页")
    }
    

    
}



/设置背景颜色
        self.view.backgroundColor = UIColor.orangeColor()

        //设置rightItem
        let item = UIBarButtonItem.init(barButtonSystemItem: .Cancel, target: self, action: "itemAction")
        self.navigationItem.rightBarButtonItem = item
        
        //1.替换系统自带的返回按钮 -> 设置leftBarButtonItem属性
        let item2 = UIBarButtonItem.init(barButtonSystemItem: .Done, target: self, action: "itemAction")
        
        //self.navigationItem.leftBarButtonItem = item2
        
        //2.隐藏系统自带的返回按钮
        self.navigationItem.hidesBackButton = true


        //定制navigationBar
        //1.设置背景颜色
        self.navigationBar.barTintColor = UIColor.blackColor()
        
        //2.设置中间的文字颜色
        //NSForegroundColorAttributeName 文字颜色的key
        self.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
        
        //3.设置填充颜色
        self.navigationBar.tintColor = UIColor.whiteColor()

你可能感兴趣的:(UINavigationItem基础)