IOS初学-自定义导航栏中的内容

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

对导航栏中进行简单的自定义操作

新建一个FourSubViewController视图控制器


self.title="FourPage"
        self.view.backgroundColor=UIColor.brown
        
        //设置一个导航栏按钮 替换原左按钮
        let leftButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.refresh, target: self, action: #selector(FourSubViewController.refresh));
        self.navigationItem.leftBarButtonItem=leftButton;
         //设置一个导航栏按钮 替换原右按钮
        let rightButton=UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.refresh, target: self, action: #selector(FourSubViewController.save));
        self.navigationItem.rightBarButtonItem=rightButton;
        //创建一个文本区
        let  label = UILabel(frame: CGRect(x: 0, y: 0, width: 180, height: 30));
        label.text="This is title!!!";
        //居中
        label.textAlignment=NSTextAlignment.center;
        //替换原标题
        self.navigationItem.titleView=label;

实现两个绑定的点击事件

@objc func refresh(){
        //创建一个弹出窗口
        let dialog=UIAlertController(title: "title", message: "content", preferredStyle: UIAlertControllerStyle.alert);
        //创建一个弹出窗口按钮
        let  dialogAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil );
        //将按钮添加到窗口中
        dialog.addAction(dialogAction);
        //显示dialog
        self.present(dialog,animated: true,completion: nil)
    }
    
    @objc func save(){
		//点击会在控制台中输出“save。。。。。。。”
        print("save.........")
    }

猜你喜欢

转载自blog.csdn.net/qq_21153627/article/details/83993520