美文网首页
7.2、初识导航

7.2、初识导航

作者: 艾希_可可 | 来源:发表于2018-06-27 11:45 被阅读4次
    导航基础
    import UIKit
    
    class OtherViewController: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.view.backgroundColor = UIColor.cyan
            //1、导航控制器中间title,两种方法都可以
    //        self.title = "One"
            self.navigationItem.title = "One"
            //2、导航条中间出了显示文字,还可以显示任意视图,显示图片
    //        let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
    //        self.view.addSubview(imageView)
    //        imageView.image = #imageLiteral(resourceName: "qd.png")
    //        imageView.contentMode = .scaleAspectFit
    //        self.navigationItem.titleView = imageView
    
            //3、导航控制器中间title颜色
            self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.purple]
    //        self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont.systemFont(ofSize: 30), NSForegroundColorAttributeName: UIColor.black]
    
            //4系统按钮edit
            let rightItem = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(pushTwoAction))
            //4.1设置右边按钮
            self.navigationItem.rightBarButtonItem = rightItem
            //这个属性不能修改标题文字的颜色
            //能够修改左右按钮的文字颜色
            self.navigationController?.navigationBar.tintColor = UIColor.red
    //4.2自定义标题按钮
            let leftBtn = UIBarButtonItem(title: "分类", style: .plain, target: self, action: #selector(clickLeft(_:)))
            //self.navigationItem.leftBarButtonItem = leftBtn
            
            //4.3自定义图标按钮
            let images = UIImage(named: "2")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)//注意图片显示方式
            let navImageBtn = UIBarButtonItem(image: images, style: .plain, target: self, action: #selector(clickLeft2))
            self.navigationItem.leftBarButtonItems = [leftBtn, navImageBtn]
            
            let DeviceWidth = UIScreen.main.bounds.width
    //        let DeviceHeight = UIScreen.main.bounds.height
            let bu = UIButton(frame: CGRect(x: 0, y: 100, width: DeviceWidth, height: 50))
            self.view.addSubview(bu)
            bu.setTitle("下一页", for: .normal)
            bu.backgroundColor = UIColor.brown
            bu.addTarget(self, action: #selector(presnetAction), for: .touchUpInside)
    //        4.4适配普通机型与X
            let navHeight = UIApplication.shared.statusBarFrame.size.height + 44.0
            print(navHeight)
            bu.frame = CGRect(x: 0, y: navHeight, width: DeviceWidth, height: 50)
            
    //        let tabHeight = UIApplication.shared.statusBarFrame.size.height>20?83:49
            
        }
    //    模态跳转
        func presnetAction() {
            let two = TwoViewController()
            let nextNav = UINavigationController(rootViewController: two)
            /*
             视图控制器翻转效果
             由下向上推出(默认模式) CoverVertical
             水平翻转 FlipHorizontal
             淡入淡出 CrossDissolve
             翻页效果 PartialCurl
             注意:如果有导航视图控制器时,翻转效果设置在导航视图控制器;没有时则设置在视图控制器。
             */
            nextNav.modalTransitionStyle = .crossDissolve
            self.present(nextNav, animated: true, completion: nil)
        }
        func clickLeft2(){
            print("clickLeft2")
        }
        //导航跳转,参数是UIBarButtonItem类型的对象
        func clickLeft(_ btn: UIBarButtonItem){
            print("clickLeft")
            let two = TwoViewController()
            self.navigationController?.pushViewController(two, animated: true)
        }
    //    自定义动画
        func pushTwoAction(){
            let two = TwoViewController()
            //1、push方法
            // 转场动画1
            UIView.beginAnimations(nil, context: nil)
            UIView.setAnimationCurve(UIViewAnimationCurve.easeInOut)
            UIView.setAnimationDuration(0.6)
    
            self.navigationController?.pushViewController(two, animated: true)
            UIView.setAnimationTransition(UIViewAnimationTransition.curlUp, for: self.navigationController!.view, cache: false)
            UIView.commitAnimations()
        }
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
        
    
        /*
        // MARK: - Navigation
    
        // In a storyboard-based application, you will often want to do a little preparation before navigation
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            // Get the new view controller using segue.destinationViewController.
            // Pass the selected object to the new view controller.
        }
        */
    
    }
    
    
    //
    //  TwoViewController.swift
    //  Nav
    //
    //  Created by hushuzhen on 2017/7/11.
    //  Copyright © 2017年 swifts. All rights reserved.
    //
    
    import UIKit
    
    class TwoViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.backgroundColor = UIColor.gray
            self.title = "Two"
        
    
    //        1.导航栏是否隐藏
            self.navigationController?.navigationBar.isHidden = false
    //        2.导航栏隐藏左边backitem,即leftbarbuttonitem
            //完全隐藏backItem//
            self.navigationItem.setHidesBackButton(true, animated: true)
    //        3.导航栏leftbarbuttonitem的颜色设置
            self.navigationController?.navigationBar.tintColor = UIColor.black
    //        8.导航栏重新定义leftbarbuttonitem
            //重新定义backItem,将覆盖原来的BackItem.与storyboard中拖入一个item,效果一样。都是覆盖原来的backitem。
    //        导航返回按钮,不做任何设置,系统默认上一页标题
    //        第一种代码定义方式,设置系统按钮
    //        self.navigationItem.setLeftBarButton(UIBarButtonItem(barButtonSystemItem: .camera, target: self, action: #selector(backToOneVC)), animated: true)
            
            //第二种代码定义方式,初始化自定义
    //        self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.add, target: self, action: #selector(backToOneVC))
            
    //        第三种代码定义的方式,自定义文字按钮
            self.navigationItem.setLeftBarButton(UIBarButtonItem(title: "< 返回", style: .plain, target: self, action: #selector(backToOneVC)), animated: true)
            //        4.导航栏leftbarbuttonitem的字体,颜色,大小设置
            self.navigationItem.leftBarButtonItem?.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.blue,NSFontAttributeName: UIFont(name: "Chalkduster", size: 15)!], for: .normal)
    //        第四种自定义图片为左按钮
    //        let leftBtns = UIBarButtonItem(image: UIImage.init(named: "2")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal), style: .plain, target: self, action: #selector(backToOneVC))
    ////        //消除左边的间隙
    //        let space = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
    //        space.width = -10
    //        self.navigationItem.leftBarButtonItems = [space,leftBtns]
            //        9.导航栏设置成透明
            //将导航栏设置成透明
            //        2.导航栏的最底部颜色设置
            //backgroundColor 是最底下的color
            self.navigationController?.navigationBar.backgroundColor = UIColor.yellow
            self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
    //        self.navigationController!.navigationBar.shadowImage = UIImage()
            self.navigationController?.navigationBar.isTranslucent = true
    
            self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .bookmarks, target: self, action: #selector(comeToNext))
        }
            func backToOneVC() {
                //2、pop方法
            self.navigationController?.popViewController(animated: true)
    //        返回根视图self.navigationController?.popToRootViewControllerAnimated(true);
        }
        func comeToNext() {
            print("点击写一页")
        }
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }
    
    

    UIViewController扩展

    
    import UIKit
    
    extension UIViewController{
        //自定义导航栏
        func changeSystemNav() {
            //隐藏系统的导航栏 不然点击事件受到影响
            self.navigationController?.isNavigationBarHidden=true
            
            // 创建一个导航栏
            let navBar = UINavigationBar(frame: CGRect(x:0, y:20, width:self.view.frame.size.width, height:60))
            // 导航栏背景颜色
            navBar.backgroundColor = UIColor.blue
            //这里是导航栏透明
            //navBar.shadowImage = UIImage()
            //navBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
            
            // 自定义导航栏的title,用UILabel实现
            let titleLabel = UILabel(frame: CGRect(x:0,y:0,width:50,height:60))
            titleLabel.text = "LOG IN"
            titleLabel.textColor = UIColor.red
            //这里使用系统自定义的字体
            //titleLabel.font = UIFont(name: "Roboto-Medium", size: 16)
            
            // 创建导航栏组件
            let navItem = UINavigationItem()
            // 设置自定义的title
            navItem.titleView = titleLabel
            
            // 创建左侧按钮
            let leftButton = UIBarButtonItem(title: "leftButton", style: .plain, target: self, action: nil)
            leftButton.tintColor = UIColor.red
            
            // 创建右侧按钮
            let rightButton = UIBarButtonItem(title: "rightButton", style: .plain, target: self, action: nil)
            rightButton.tintColor = UIColor.red
            
            // 添加左侧、右侧按钮
            navItem.setLeftBarButton(leftButton, animated: false)
            navItem.setRightBarButton(rightButton, animated: false)
            
            navigationItem.setHidesBackButton(true, animated: false)
            // 把导航栏组件加入导航栏
            navBar.pushItem(navItem, animated: false)
            
            // 导航栏添加到view上
            self.view.addSubview(navBar)
        }
        
    }
    

    相关文章

      网友评论

          本文标题:7.2、初识导航

          本文链接:https://www.haomeiwen.com/subject/ftjbyftx.html