最近一年左右放弃使用xib来布局页面,纯代码布局中使用masonry也基本满足开发需求,只是有些xib中很好用的一些写法,用的不太熟悉,有时候难免写了很多冗余代码
还有就是在一些复杂一点儿的布局中直还是会出现一些小瑕疵,而使用系统提供的布局方式,没有出现问题,
11
1.xib中的视图1和视图2可设置比例来做适配 这个可以用autolayout来实现两个视图宽度的比例
NSLayoutConstraint *b_consWidth = [NSLayoutConstraint constraintWithItem:self.leftView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.infoView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0.0];
[self.stackView addConstraint:b_consWidth];
同样的效果,用masonry来实现
// [self.leftView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.width.equalTo(_infoView.mas_width).multipliedBy(0.5);
// }];
2.设置约束优先级,来控制视图的显示和隐藏
在实际的开发中如果有view1,view2,view3线性布局并左对齐,如果数据源中view2中的数据为空,则view2是需要被从视图中移除,view3要自动向view1的位置靠拢,自适应布局
在xib中拖线实现可以先设置view1,view2,view3的常规布局,
再设置一个view2被移除之后view3和view1的靠拢的约束,在xib中这个约束设置之后,约束线会自动变成红色,表明这条约束和原有的约束是有冲突的,在这样的情况下,将最后设置的约束的优先级数值改小,默认系统的优先级是从0-1000,改成一个小于1000的值即可满足需求
使用masonry来实现就需要在view的数据进行赋值的时候再讲frame更新一下,这个就有点儿令人难受了
在接下来的一个使用场景中:
12
游戏段位,擅长位置,擅长英雄,游戏范围,可接大区的布局是根据数据来做展示和隐藏,而背景的白色区域和底部的大神协议都是根据数据来自适应变化,还会根据用户操作来做展示和隐藏的变动,目测使用UIStackView来实现能够从内到外来实现UI布局
附UIStackView简单用法
#import "ViewController.h"
#import "Masonry.h"
#import "PinkViewCtl.h"
#import "SDWebImage.h"
#import "YZNeedSubModel.h"
#import "YZMessageModel.h"
#import "MJExtension.h"
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
@interface ViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIView * bgScrollView;
@property(nonatomic,strong)UIView * yzclientZBView;
@property(nonatomic,strong)UIView * tabBgView;
@property(nonatomic,strong)PinkViewCtl * vc;
@property(nonatomic,assign)CGFloat greenHeight;
@property(nonatomic,assign)CGFloat redheight;
@property(nonatomic,strong)NSMutableArray * emojArray;
@property(nonatomic,strong)UIStackView * stackView;
@property(nonatomic,strong)UIView * infoView;
@property(nonatomic,strong)UIView * leftView;
@property(nonatomic,strong)UIView * middleView;
@property(nonatomic,strong)UIView * rightView;
@end
@implementation ViewController
-(UIView *)infoView {
if (_infoView == nil) {
_infoView = [[UIView alloc] init];
_infoView.backgroundColor = [UIColor redColor];
_infoView.translatesAutoresizingMaskIntoConstraints = NO;
}
return _infoView;
}
-(UIView *)leftView {
if (_leftView == nil) {
_leftView = [[UIView alloc] init];
_leftView.backgroundColor = [UIColor greenColor];
_leftView.translatesAutoresizingMaskIntoConstraints = NO;
}
return _leftView;
}
-(UIView *)middleView {
if (_middleView == nil) {
_middleView = [[UIView alloc] init];
_middleView.backgroundColor = [UIColor blueColor];
_middleView.translatesAutoresizingMaskIntoConstraints = NO;
}
return _middleView;
}
-(UIView *)rightView {
if (_rightView == nil) {
_rightView = [[UIView alloc] init];
_rightView.backgroundColor = [UIColor systemPinkColor];
_rightView.translatesAutoresizingMaskIntoConstraints = NO;
}
return _rightView;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.stackView = [[UIStackView alloc] init];
[self.view addSubview:self.stackView];
//
self.stackView.axis = UILayoutConstraintAxisHorizontal;
self.stackView.spacing = 0;
self.stackView.alignment = UIStackViewAlignmentFill;
// self.stackView.distribution = UIStackViewDistributionFillEqually;
[self.stackView addArrangedSubview:self.infoView];
[self.stackView addArrangedSubview:self.leftView];
[self.stackView addArrangedSubview:self.middleView];
[self.stackView addArrangedSubview:self.rightView];
[self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.right.bottom.equalTo(self.view);
}];
NSLayoutConstraint *b_consWidth = [NSLayoutConstraint constraintWithItem:self.leftView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.infoView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0.0];
[self.stackView addConstraint:b_consWidth];
// [self.leftView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.width.equalTo(_infoView.mas_width).multipliedBy(0.5);
// }];
[self.middleView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(_leftView.mas_width);
}];
[self.rightView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(_middleView.mas_width);
}];
// [self.leftView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.width.equalTo(self.middleView.mas_width);
// }];
//
// [self.middleView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.width.equalTo(self.rightView.mas_width);
// }];
}
始终觉得xib用着顺手,但是确实有一种感觉,使用纯代码实现能提升自己对这个布局的理解程度
-------------------国庆假期快乐~~
补充:后来发现Masonry也能设置视图的宽高比和两个视图之间的宽度比例
见https://www.jianshu.com/p/893a09926b81?nomobile=yes















网友评论