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

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

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

在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;
    }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值