使用第三方框架 Keep Layout 實現簡單的自動布局


之前有寫了一篇Masonry實現自動布局的隨筆,今天無聊的時候又發現了另外一個自動布局的第三方框架Keep Layout,使用起來感覺比Masonry簡單一點,相比之下容易理解?

附上源碼地址:https://github.com/iMartinKiss/KeepLayout

關於一些屬性的介紹:

 

屬性間的關系以及優先級:

更多內容請參考網站說明。

附上簡單自動布局的代碼以及效果圖:

 1 - (void)viewDidLoad {
 2     [super viewDidLoad];
 3     // Do any additional setup after loading the view, typically from a nib.
 4     
 5     [self setUpFourViews];
 6    
 7 }
 8 - (void)setUpFourViews {
 9     
10     UIView *redView = [[UIView alloc] init];
11     
12     redView.backgroundColor = [UIColor redColor];
13     [self.view addSubview:redView];
14     
15     
16     UIView *blueView = [[UIView alloc] init];
17     blueView.backgroundColor = [UIColor blueColor];
18     [self.view addSubview:blueView];
19     
20     UIView *greenView = [[UIView alloc] init];
21     
22     greenView.backgroundColor = [UIColor greenColor];
23     [self.view addSubview:greenView];
24     
25     
26     UIView *purpleView = [[UIView alloc] init];
27     purpleView.backgroundColor = [UIColor purpleColor];
28     [self.view addSubview:purpleView];
29     
30     [self testWithRedView:redView blueView:blueView];
31     
32     [self testWithGreenView:greenView purpleView:purpleView];
33 
34 }
35 - (void)testWithGreenView:(UIView *)greenView purpleView:(UIView *)purpleView {
36     
37     greenView.keepBottomInset.equal = 30;//離父view的底部間距為30
38     greenView.keepLeftInset.equal = 30;//離父view的左間距為30
39     greenView.keepRightOffsetTo(purpleView).equal = 30;//離purpleView的右邊間距為30
40     greenView.keepHeight.equal = 50;//高度為50
41     greenView.keepWidthTo(purpleView).equal = 1;//寬度和purpleView相等
42     
43     purpleView.keepBottomAlignTo(greenView).equal = 1;//和greenView的底部對齊
44     purpleView.keepRightInset.equal = 30;//離父view的右間距為30
45     purpleView.keepHeightTo(greenView).equal = 2;//高度是greenView的2倍
46 }
47 
48 - (void)testWithRedView:(UIView *)redView blueView:(UIView *)blueView {
49     
50     redView.keepTopInset.equal = 30;//離父view的頂部間距為30
51     redView.keepRightInset.equal = 30;//離父view的右間距為30
52     redView.keepLeftInset.equal = 30;//離父view的左間距為30
53     redView.keepHeight.equal = 50;
54     
55     blueView.keepLeftInset.equal = 30;//離父view的左間距為30
56     blueView.keepRightInset.equal = 30;//離父view的右間距為30
57     blueView.keepTopOffsetTo(redView).equal = 30;//離redView的頂部間距為30
58     blueView.keepHeightTo(redView).equal = 1;//高度和redView的相等
59 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM