[Android開發學iOS系列] 快速上手UIKit

快速上手iOS UIKitUIKit是蘋果官方的framework, 其中包含了各種UI組件, window和view, 事件處理, 交互, 動畫, 資源管理等基礎設施支持.
按照前面的介紹, 用UIKit寫UI可以用storyboard(Interface Builder)和代碼兩種方式.
大體的思路都是添加組件后, 設置屬性, 設置尺寸位置約束, 處理響應事件.
這里主要介紹用代碼寫的情形.希望這篇文章, 可以幫你快速上手UIKit, 熟悉常用的組件, 完成一些簡單的UI界面相關任務.
在代碼中寫UI的基本步驟在代碼中寫UI的步驟大致是:

  • 初始化.
  • addSubview添加到當前view, 或hierarchy中的其他可達view.
  • 設置約束.
比如:
class ViewController: UIViewController {var myLabel: UILabel!override func loadView() {view = UIView()view.backgroundColor = .white// 創建實例myLabel = UILabel()myLabel.translatesAutoresizingMaskIntoConstraints = falsemyLabel.text = "Hello"http:// 添加到view中view.addSubview(myLabel)// 設置約束NSLayoutConstraint.activate([myLabel.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),myLabel.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor),])}}這里有幾點說明:
  • var** myLabel: UILabel! 組件字段這樣聲明有lateinit的作用, 如果不帶!會報錯, 說controller沒有init方法.
  • 如果在代碼中設置UI組件的constraints, 那么這個屬性經常要設置為false: translatesAutoresizingMaskIntoConstraints = **false**. 如果組件的位置是通過frame來設置的, 則不用設置這個屬性.
  • 約束有多種寫法, 這里只是其中一種, 用anchor的方式.
常用組件文字: UILabel設置文字等屬性:
myLabel = UILabel()myLabel.translatesAutoresizingMaskIntoConstraints = falsemyLabel.font = UIFont.systemFont(ofSize: 24)myLabel.text = "Hello"myLabel.numberOfLines = 0myLabel.textAlignment = .right給UILabel設置點擊事件:
myLabel.isUserInteractionEnabled = truelet tapGesture = UITapGestureRecognizer(target: self, action: #selector(userDidTapLabel(tapGestureRecognizer:)))myLabel.addGestureRecognizer(tapGesture)點擊事件處理方法:
【[Android開發學iOS系列] 快速上手UIKit】@objc func userDidTapLabel(tapGestureRecognizer _: UITapGestureRecognizer) {print("label clicked!")}這里有#selector, 對應的userDidTapLabel方法要加上@objc. 便于OC的代碼調用能找到swift的方法.
給UILabel設置點擊事件和UIButton不同, 這點我們后面說繼承關系的時候解釋一下.
按鈕: UIButton設置文字:
submitButton = UIButton(type: .system)submitButton.translatesAutoresizingMaskIntoConstraints = falsesubmitButton.titleLabel?.font = UIFont.systemFont(ofSize: 36)submitButton.setTitle("SUBMIT", for: .normal)submitButton.setTitleColor(.black, for: .normal)設置點擊事件:
submitButton.addTarget(self, action: #selector(submitTapped), for: .touchUpInside)@objc func submitTapped(_ sender: UIButton) {}這里使用@objc的理由同上.
基本上我們在iOS代碼中用到#的時候, 對應的方法都要加上@objc.
輸入框: UITextFieldmyTextField = UITextField()myTextField.translatesAutoresizingMaskIntoConstraints = falsemyTextField.placeholder = "What's your name?"myTextField.textAlignment = .centermyTextField.font = UIFont.systemFont(ofSize: 44)想要禁用輸入框可以這樣:
myTextField.isUserInteractionEnabled = false彈框在app里簡單的交互我們經常需要彈出一個對話框:
let alert = UIAlertController(title: "title", message: "message", preferredStyle: .alert)alert.addAction(UIAlertAction(title: "Ok", style: .default))alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))present(alert, animated: true)其中preferredStyle有.alert.actionSheet兩種.
.alert是中心的對話框, 一般用于信息提示或者確認操作; .actionSheet是底部的bottom sheet, 一般用來在幾個選項中做選擇.
其他
  • view中比較常用的屬性isHidden, 控制view是否需要隱藏.
  • 所有的UIView都有一個layer屬性.設置border的寬度和顏色就在layer上設置.CALayer在UIView之下. 所以不知道UIColor, 只知道CGColor.
本文僅列出幾個常用組件, 更多的請看官方示例.
這里可以下載
繼承關系NSObject是所有Cocoa Touch class的基類. 所有UIKit中的類都是它的子類.
這里有一個類關系的圖:
[Android開發學iOS系列] 快速上手UIKit

文章插圖
我們這里不展開講述所有了, 只解答一下前面提出的關于UILabel點擊事件的問題.

推薦閱讀