IOS初學-自定義導航欄中的內容
阿新 • • 發佈:2018-11-16
對導航欄中進行簡單的自定義操作
新建一個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.........") }