IOS初学-导航视图控制器的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21153627/article/details/83991167

视图控制器的跳转方式。入栈和出栈

首先需要创建两个视图控制器

在FirstSubViewController中修改代码

//导航视图控制器的使用
    func test1()  {
        self.title="onePage"
        self.view.backgroundColor=UIColor.brown;
        //设置右上角导航按钮的样式和功能。当点击按钮时 页面跳转至第二个视图控制器
        self.navigationItem.rightBarButtonItem=UIBarButtonItem(title: "Next", style: UIBarButtonItemStyle.plain, target: self, action: #selector(FirstSubViewController.nextPage));
    }
    @objc func nextPage(){
        //初始化第二个视图控制器
        let twoPage=SecondSubViewController();
        //将第二个视图压入导航控制器 实现页面跳转
        self.navigationController?.pushViewController(twoPage, animated: true);
    }

在SecondSubViewController中修改代码

self.view.backgroundColor=UIColor.purple;
        self.title="222222"

在FirstSubViewController中编辑代码

func test1()  {
        self.title="onePage"
        self.view.backgroundColor=UIColor.brown;
        //设置右上角导航按钮的样式和功能。当点击按钮时 页面跳转至第二个视图控制器
        self.navigationItem.rightBarButtonItem=UIBarButtonItem(title: "Next", style: UIBarButtonItemStyle.plain, target: self, action: #selector(FirstSubViewController.nextPage));
    }
    @objc func nextPage(){
        //初始化第二个视图控制器
        let twoPage=SecondSubViewController();
        //将第二个视图压入导航控制器 实现页面跳转
        self.navigationController?.pushViewController(twoPage, animated: true);
    }

最后设置程序的入口

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        let navigationController=UINavigationController(rootViewController: FirstSubViewController());
        self.window?.rootViewController=navigationController;
        return true
    }

在上一版代码的基础上  增加了隐藏工具栏和导航的代码

在FirstSubViewController中重写方法

//视图控制器 生命周期中 视图即将显示的方法
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated);
        //显示工具栏
        self.navigationController?.setToolbarHidden(false, animated: true);
    }

SecondSubViewController中添加隐藏的button的方法

let button1=UIButton(frame: CGRect(x: 40, y: 200, width: 240, height: 30));
        button1.setTitle("hide navigation bar", for: UIControlState());
        button1.backgroundColor=UIColor.orange;
        button1.addTarget(self, action: #selector(SecondSubViewController.hideNavigationBar), for: UIControlEvents.touchUpInside);
        self.view.addSubview(button1);

        let button2=UIButton(frame: CGRect(x: 40, y: 260, width: 240, height: 30));
        button2.setTitle("hide tool bar", for: UIControlState());
        button2.backgroundColor=UIColor.orange;
        button2.addTarget(self, action: #selector(SecondSubViewController.hideToolBar), for: UIControlEvents.touchUpInside);
        self.view.addSubview(button2);
//隐藏顶部导航栏
    @objc func hideNavigationBar(){
        self.navigationController?.setNavigationBarHidden(true, animated: true);
    }
    //隐藏底部工具栏
    @objc func hideToolBar(){
        self.navigationController?.setToolbarHidden(true, animated: true);
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

修改导航栏中的样式

直接在上一版中的代码进行修改。在viewWillAppear方法中

//视图控制器 生命周期中 视图即将显示的方法
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated);
        //显示工具栏
        self.navigationController?.setToolbarHidden(false, animated: true);
        self.navigationItem.prompt="这里是标题文字"
        //设置导航栏的背景不透明
        self.navigationController?.navigationBar.isTranslucent=false;
        //设置导航栏的样式为黑色
        self.navigationController?.navigationBar.barStyle=UIBarStyle.black;
        //设置导航按钮的前景色
        self.navigationController?.navigationBar.tintColor=UIColor.orange;
    }

猜你喜欢

转载自blog.csdn.net/qq_21153627/article/details/83991167
今日推荐