12.swift5-常用UI組件

UIStoryboard

//1.通過stroyboard獲取控制器

let childVc = UIStoryboard(name: "Home", bundle: nil).instantiateInitialViewController()!

頂部導航欄UIBarButtonItem

controller繼承UIViewController,子類有navigationItem(UINavigationItem)

let btn = UIButton()

btn.setImage(UIImage(named: "logo"), for: UIControl.State.normal)//logo是圖片資源名

//swift的圖片資源不用寫路徑名,要求圖片資源唯一

頂部導航欄左邊按鈕:navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)

頂部導航欄右邊按鈕數組navigationItem.rightBarButtonItems = []

UITapGestureRecognizer:

假如你想要在你的應用中檢測手勢,例如點擊,縮放,平移,或者旋轉,用Swift和內建的UIGestureRecognizer類實現是非常容易的

let tapGes = UITapGestureRecognizer(target: self, action: #selector(self.titleLabelClick(tagGes:)))

label.addGestureRecognizer(tapGes)

//獲取手勢所在view

@objc private func titleLabelClick(tagGes: UITapGestureRecognizer){

guard let currentLabel = tagGes.view as? UILabel else {return}

}

//獲取位置

func picTap(sender: UITapGestureRecognizer) {

let point = sender.location(in: sender.view)

}

alert對話框

注意,最後要present才會彈出對話框,然後是一個alertController裡面加alertAction,可以有多個alertAction

let alertController = UIAlertController(title:"提示", message:"學生賬號註冊暫未開放z", preferredStyle: .alert)

let alertOK = UIAlertAction(title: "好的", style: .cancel, handler:nil)

alertController.addAction(alertOK)

present(alertController, animated:true, completion: nil)

UITextField

UITextField加placeholder:

textField.attributedPlaceholder = NSAttributedString.init(string:"請輸入用戶名", attributes: [

NSAttributedString.Key.foregroundColor:UIColor.lightGray])


分享到:


相關文章: