iOS UI系列 (四) :可复用的Xib(1) 静态内容
有时候页面中的部分内容相同,或者是一些静态的内容组合,这时候我们就可以把这些见面封装到一个XIB里
新建Single View Application
新建一个View.Xib
- Command+N–>User Interface–>View
- 把界面大小改为Freeform
- 添加一个UILabel, 两个UIView, 并设置对应的背景色
- 添加对应的约束,让两个UIView等宽,且Space都是10, 高度固定,且与周围的约速为10, 对UILabel也设置对应的约速,细节就不写了,看图

使用Xib
在ViewController添加 一个UIView, 并设置对应的约束,连接这个UIView为Controller的 IBOutlet ContainerView

- 在ViewDidLoad里添加如下代码
1 | super.viewDidLoad() |
运行

- 添加约束
1 | import UIKit |
containerView.addSubview(v)
1 | setUpConstraint() |
// Do any additional setup after loading the view, typically from a nib.
}
1 | override func didReceiveMemoryWarning() { |
1 | func setUpConstraint() |
3. 运行结果, 我们的Xib已经可以自适应容器了


iOS UI系列 (四) :可复用的Xib(1) 静态内容
版权:本文版权归作者所有,转载需经作者同意。
# Related Post
1.iOS UI系列 (三) :Reusable Button
2.iOS UI系列 (二) :使用多个StoryBoard
3.iOS UI系列 (一) :Auto Layout 高度三等分
4.iOS开发(一):真机调试
1.iOS UI系列 (三) :Reusable Button
2.iOS UI系列 (二) :使用多个StoryBoard
3.iOS UI系列 (一) :Auto Layout 高度三等分
4.iOS开发(一):真机调试