Ios

Autolayout用のiOSMasonryサードパーティフレームワーク



Ios Masonry Third Party Framework




I.はじめに:

  • 最も人気のあるAutolayoutサードパーティフレームワーク
  • エレガントなコードで自動レイアウトを書く
  • Appleの公式の複雑な自動レイアウトコードを保存しました
  • 開発効率が大幅に向上

次に、フレームアドレス:

https://github.com/snapKit/Masonry

第三に、例:

// margin constraint: -(void)test1{ // 1. Red View UIView *redView= [[UIView alloc] init] redView.backgroundColor = [UIColor redColor] [self.view addSubview:redView] // 2.1 add constraints: default mutipliedBy = 1.0 can not write // [redView mas_makeConstraints:^(MASConstraintMaker *make) { // make.top.equalTo(self.view.mas_top).multipliedBy(1.0).offset(20) // make.bottom.equalTo(self.view.mas_bottom).multipliedBy(1.0).offset(-20) // make.left.equalTo(self.view.mas_left).multipliedBy(1.0).offset(20) // make.right.equalTo(self.view.mas_right).multipliedBy(1.0).offset(-20) // }] // 2.2 can be simplified: // [redView mas_makeConstraints:^(MASConstraintMaker *make) { // make.top.equalTo(self.view.mas_top).offset(20) // make.bottom.equalTo(self.view.mas_bottom).offset(-20) // make.left.equalTo(self.view.mas_left).offset(20) // make.right.equalTo(self.view.mas_right).offset(-20) // }] // 2.3 can be simplified again: // [redView mas_makeConstraints:^(MASConstraintMaker *make) { // make.top.offset(20) // make.bottom.offset(-20) // make.left.offset(20) // make.right.offset(-20) // }] // 2.4 can be simplified again: // [redView mas_makeConstraints:^(MASConstraintMaker *make) { // make.top.and.left.offset(20) // make.bottom.and.bottom.offset(-20) // }] // 2.5 can be simplified again: // [redView mas_makeConstraints:^(MASConstraintMaker *make) { // make.top.left.offset(20) // make.bottom.bottom.offset(-20) // }] // 2.6 can be simplified again: // [redView mas_makeConstraints:^(MASConstraintMaker *make) { // make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(20, 20, 20, 20)) // }] // 2.7 can be simplified again: [redView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.insets(UIEdgeInsetsMake(20, 20, 20, 20)) }] } //Center constraint: -(void)test2{ // 1. Red View UIView *redView= [[UIView alloc] init] redView.backgroundColor = [UIColor redColor] [self.view addSubview:redView] // 2.1 add constraints // [redView mas_makeConstraints:^(MASConstraintMaker *make) { // make.width.equalTo(@100) // make.height.equalTo(@100) // make.centerX.equalTo(self.view.mas_centerX) // make.centerY.equalTo(self.view.mas_centerY) // }] // 2.2 Add constraints: Automatically convert to the appropriate type A very large enumeration // [redView mas_makeConstraints:^(MASConstraintMaker *make) { // make.width.mas_equalTo(100) // make.height.mas_equalTo(100) // make.centerX.mas_equalTo(self.view.mas_centerX) // make.centerY.mas_equalTo(self.view.mas_centerY) // }] // 2.3 Add constraints [redView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(100, 100)) make.center.mas_equalTo(self.view) }] } / / Comprehensive example: -(void)test3{ // 1. Red View UIView *redView= [[UIView alloc] init] redView.backgroundColor = [UIColor redColor] [self.view addSubview:redView] // 2. Blue View UIView *blueView = [[UIView alloc]init] blueView.backgroundColor = [UIColor blueColor] [self.view addSubview:blueView] // // 3. Blue View constraint // [blueView mas_makeConstraints:^(MASConstraintMaker *make) { // make.left.mas_equalTo(self.view.mas_left).offset(30) // make.bottom.mas_equalTo(self.view.mas_bottom).offset(-30) // make.right.mas_equalTo(redView.mas_left).offset(-30) // make.width.mas_equalTo(redView.mas_width) // make.height.mas_equalTo(50) // }] // // // 4. Red View constraint // [redView mas_makeConstraints:^(MASConstraintMaker *make) { // make.right.mas_equalTo(self.view.mas_right).offset(-30) // make.top.mas_equalTo(blueView.mas_top) // make.bottom.mas_equalTo(blueView.mas_bottom) // }] // 3.1 Blue View Constraint: Add Macro Can Remove mas [blueView makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view.left).offset(30) make.bottom.equalTo(self.view.bottom).offset(-30) make.right.equalTo(redView.left).offset(-30) make.width.equalTo(redView.width) make.height.equalTo(50) }] // 4.1 Red View constraint: Add macro Can remove mas [redView makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.view.right).offset(-30) make.top.equalTo(blueView.top) make.bottom.equalTo(blueView.bottom) }] // Update constraints: [blueView mas_makeConstraints:^(MASConstraintMaker *make) { make.height.equalTo(80) }] / / Remove all the constraints added before, re-add the following constraints // [blueView remakeConstraints:^(MASConstraintMaker *make) { // // }] //with and can be written arbitrarily without affecting use }