最近項目以 Storyboard 為主,代碼量減少了不少,然后總結(jié)了一些可用的小技巧。
1.可視化坐標(biāo)距離
選中一個 View,然后按住 option 并將鼠標(biāo)移動到其他 View,即可看到對應(yīng)的距離

間距
2.添加輔助線
添加:點擊某個 View,按下 shift+Command+- 添加橫向輔助線,shift+Command+| 添加縱向輔助線,添加的位置都是左右/上下居中的
或者選擇(感覺作用不大)

輔助線
移動:光標(biāo)移動到線上時會出現(xiàn)可拖動的按鈕,按住 左右/上下 拖動到想要的位置,拖動時可以看到輔助線線距離視圖 左右/上下 的距離
刪除:拖動到視圖以外的地方即可刪除,類似于斷點刪除
3.清除某個視圖的所有約束

清除約束
4.拖線方式
如果直接左右會顯得太窄,可以設(shè)置上下布局
如果沒有自動展示對應(yīng)的 View 可以按住 option 鍵然后點擊需要打開的文件即可

自動選擇

布局方式
5.利用 @IBInspectable 減少代碼設(shè)置
@IBInspectable var borderColor: UIColor? {
get {
guard let color = layer.borderColor else { return nil }
return UIColor(cgColor: color)
}
set {
guard let color = newValue else {
layer.borderColor = nil
return
}
// Fix React-Native conflict issue
guard String(describing: type(of: color)) != "__NSCFType" else { return }
layer.borderColor = color.cgColor
}
}
設(shè)置后 Storyboard 則會出現(xiàn)

IBInspectable
6.獲取控制器
首先需要給每個控制器設(shè)置 Storyboard ID

Storyboard ID
然后獲?。?/p>
// 先獲取 Storyboard
let sb = UIStoryboard(name: "Main", bundle: nil)
let vc = sb.instantiateViewController(withIdentifier: "ViewController") as? ViewController
7.UITableView 靜態(tài)表
布局靜態(tài)表的時候只能使用 UITableViewController
選中 TableView 視圖,在右側(cè)屬性欄 - Table View 欄目下修改 Content 的類型為 Static Cells 即可設(shè)置 cell
8.選擇對齊視圖
我們可以選擇是安全區(qū)域還是整個 View 以下。

區(qū)域選擇