目錄:[Swift]Xcode實際操做html
本文將演示設置導航按鈕的樣式,以及設置導航標題區域的樣式。app
1 import UIKit 2 3 class FirstSubViewController: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 8 // Do any additional setup after loading the view. 9 self.title = "First Page" 10 self.view.backgroundColor = UIColor.brown 11 12 //實例化一個工具條按鈕對象,它將做爲新的導航按鈕 13 let leftBar = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.refresh, target: self, action: #selector(FirstSubViewController.refresh)) 14 //將導航欄左側按鈕,設置爲新建的工具條按鈕對象 15 self.navigationItem.leftBarButtonItem = leftBar 16 17 //一樣爲導航欄的右側導航按鈕,設置新的樣式 18 let rightBar = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.save, target: self, action: #selector(FirstSubViewController.save)) 19 //將導航欄的右側按鈕,設置爲新建的工具條按鈕對象 20 self.navigationItem.rightBarButtonItem = rightBar 21 22 //新建一個標籤對象,它將顯示標題區 23 let label = UILabel(frame: CGRect(x: 0, y: 0, width: 180, height: 30)) 24 //設置標籤對象的文字內容 25 label.text = "Happy Day" 26 //將標籤對象的文字對齊方式,設置爲居中對齊。 27 label.textAlignment = NSTextAlignment.center 28 //將標籤視圖對象,設置爲導航欄的標題區 29 self.navigationItem.titleView = label 30 } 31 32 //建立左側導航按鈕的點擊事件 33 @objc func refresh() 34 { 35 //建立一個警告彈出窗口 36 let alert = UIAlertController(title: "Infomation", message: "Refresh my feeling.", preferredStyle: UIAlertController.Style.alert) 37 //建立一個按鈕,做爲提示窗口中的【肯定】按鈕, 38 //當用戶點擊該按鈕時,將關閉提示窗口 39 let action = UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil) 40 //將肯定按鈕,添加到提示窗口中 41 alert.addAction(action) 42 //在當前視圖控制器中,展現提示窗口 43 self.present(alert, animated: true, completion: nil) 44 } 45 46 //建立右側導航按鈕的點擊事件 47 @objc func save() 48 { 49 //當用戶點擊按鈕時,在控制檯打印輸出日誌 50 print("Saving...") 51 } 52 53 override func didReceiveMemoryWarning() { 54 super.didReceiveMemoryWarning() 55 // Dispose of any resources that can be recreated. 56 } 57 }