以前有寫了一篇Masonry實現自動佈局的隨筆,今天無聊的時候又發現了另一個自動佈局的第三方框架Keep Layout,使用起來感受比Masonry簡單一點,相比之下容易理解?git
附上源碼地址:https://github.com/iMartinKiss/KeepLayoutgithub
關於一些屬性的介紹:框架
屬性間的關係以及優先級:佈局
更多內容請參考網站說明。網站
附上簡單自動佈局的代碼以及效果圖:spa
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 }