git项目demo地址:https://github.com/chenwei007/CWMasonryDemo
1,当有需要动态隐藏和显示控件时更新约束(控件高度不确定)
UIView *oneBackView = [[UIView alloc]init];
oneBackView.backgroundColor = [UIColor redColor];
self.oneBackView = oneBackView;
[self.view addSubview:oneBackView];
[oneBackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view);
make.top.equalTo(self.view).offset(100);
}];
__weak typeof(self) weakSelf = self;
UIView *twoBackView = [[UIView alloc]init];
twoBackView.backgroundColor = [UIColor blueColor];
twoBackView.alpha = 0.3;
self.twoBackView = twoBackView;
[self.view addSubview:twoBackView];
[twoBackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view);
weakSelf.topConstraint = make.top.equalTo(oneBackView.mas_bottom);
}];
说明这两个view是兄弟关系,当隐藏前一个,后一个顶上去,当不隐藏时,第一个显示,后一个继续跟着第一个view。
解决方法,把第二个view的top约束保存下来,当要改变约束时调用方法
[self.topConstraint uninstall];
if (isHidden) {
self.oneBackView.hidden = YES;
[self.twoBackView mas_updateConstraints:^(MASConstraintMaker *make) {
self.topConstraint = make.top.equalTo(self.view).offset(100);
}];
}else{
self.oneBackView.hidden = NO;
[self.twoBackView mas_updateConstraints:^(MASConstraintMaker *make) {
self.topConstraint = make.top.equalTo(self.oneBackView.mas_bottom);
}];
}
[UIView animateWithDuration:0.1 animations:^{
[self.view layoutIfNeeded];
}];
2,当有需要动态隐藏和显示控件时更新约束(控件的高度知道)
此时不需要保存约束,直接更新控件的高度即可,隐藏时更新高度为0,不隐藏时更新高度为控件的高度。
3,cell上控件的约束设置问题
最后一个控件一定要设置它的底部与cell.contentview的关系 如 make.bottom.equalTo(self.contentView);
在设置整块区域时,最底部加一条分割线,当分割线的高度为小于1时,masorny算的不准
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2017-06-10 20:07:43.651 QeelinGold-iOS[15116:7237790] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<MASLayoutConstraint:0x145f368a0 UIImageView:0x145f35990.height == 100>",
"<MASLayoutConstraint:0x145f36b90 UIImageView:0x145f35990.top == UITableViewCellContentView:0x145ea9080.top + 4>",
"<MASLayoutConstraint:0x145f39c40 UIView:0x145f390c0.height == 0.5>",
"<MASLayoutConstraint:0x145f37b50 UIView:0x145f390c0.top == UIImageView:0x145f35990.bottom>",
"<MASLayoutConstraint:0x145f3a180 UIView:0x145f390c0.bottom == UITableViewCellContentView:0x145ea9080.bottom>",
"<NSLayoutConstraint:0x145eaac00 UITableViewCellContentView:0x145ea9080.height == 104.667>"
)
Will attempt to recover by breaking constraint
<MASLayoutConstraint:0x145f368a0 UIImageView:0x145f35990.height == 100>
分割线设置的是0.5,但整块区域的高度时104.667,注意不是104.5。我的解决方案时把分割线封装到view上,里面给一条0.5高度的分割线。
4,scrollview上的控件约束问题(srollview上的控件的高度动态)
UIScrollView *mainContentScrollView = [[UIScrollView alloc]init];
self.mainContentScrollView = mainContentScrollView;
mainContentScrollView.backgroundColor = [UIColor grayColor];
mainContentScrollView.showsHorizontalScrollIndicator = YES;
mainContentScrollView.showsVerticalScrollIndicator = YES;
mainContentScrollView.bounces = YES;
mainContentScrollView.delegate = self;
[self.view addSubview:mainContentScrollView];
[mainContentScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
UIView* contentView = UIView.new;
contentView.backgroundColor = [UIColor blueColor];
[self.mainContentScrollView addSubview:contentView];
[contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.mainContentScrollView);
make.width.equalTo(self.mainContentScrollView);
}];
// 第一种scrollview上放很多view,高度动态
UIView *lastView;
for (int i = 0; i < 10; i ++) {
UIView *oneView = [[UIView alloc]init];
oneView.backgroundColor = [UIColor redColor];
[contentView addSubview:oneView];
[oneView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(contentView);
make.top.equalTo(lastView ? lastView.mas_bottom : @0).offset(20);
make.height.equalTo(@100);
}];
lastView = oneView;
}
[contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(lastView.mas_bottom);
}];
//第二种scrollview上放的有view 也有tableview,高度动态 未解决,如果有哪位可以解决这个请告知我
// UIView *oneView = [[UIView alloc]init];
// oneView.backgroundColor = [UIColor redColor];
// [contentView addSubview:oneView];
// [oneView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.left.right.equalTo(contentView);
// make.top.equalTo(contentView).offset(20);
// make.height.equalTo(@100);
// }];
//
// [contentView addSubview:self.mTableView];
// [self.mTableView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.top.equalTo(oneView.mas_bottom).offset(50);
// make.left.right.equalTo(contentView);
// make.height.equalTo(@(500));
// }];
//
// [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.bottom.equalTo(self.mTableView.mas_bottom);
// }];
5,多个控件排列在父控件上,等宽或者等高
UIView * bgView = UIView.new;
bgView.backgroundColor = [UIColor blueColor];
[self.view addSubview:bgView];
CGFloat padding = 10.0f;
[bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.view.mas_centerY);
make.centerX.equalTo(self.view.mas_centerX);
make.right.equalTo(self.view).offset(-100);
}];
UIView * redView = UIView.new;
redView.backgroundColor = [UIColor redColor];
[bgView addSubview:redView];
UIView * blackView = UIView.new;
blackView.backgroundColor = [UIColor blackColor];
[bgView addSubview:blackView];
UIView * greenView = UIView.new;
greenView.backgroundColor = [UIColor greenColor];
[bgView addSubview:greenView];
UIView * yellowView = UIView.new;
[bgView addSubview:yellowView];
yellowView.backgroundColor = [UIColor yellowColor];
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(bgView).offset(padding);
make.centerY.equalTo(bgView);
make.bottom.lessThanOrEqualTo(bgView);
/**
* 长宽相等 注意,这里不能用 make.height.equalTo(make.width);
*/
make.height.equalTo(@(100)); /// 约束长度等于宽度
make.width.equalTo(blackView.mas_width);
}];
[blackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(bgView);
make.left.equalTo(redView.mas_right).offset(padding);
make.height.equalTo(@(200));
make.width.equalTo(greenView.mas_width);
make.bottom.lessThanOrEqualTo(bgView);
}];
[greenView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(bgView);
make.left.equalTo(blackView.mas_right).offset(padding);
make.right.equalTo(yellowView.mas_left).offset(-padding);
make.height.equalTo(@(50));
make.width.equalTo(yellowView.mas_width);
make.bottom.lessThanOrEqualTo(bgView);
}];
[yellowView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(bgView);
make.right.equalTo(bgView).offset(-padding);
make.height.equalTo(@(20));
make.width.equalTo(redView.mas_width);
make.bottom.lessThanOrEqualTo(bgView);
}];
6,invalidateIntrinsicContentSize的用法
7,设置控件优先级
Hugging-保持view的size不让其变大,content Hugging默认为250.
- (void)setContentHuggingPriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);
Compression-保持view的size不让其变小, 默认为750
- (void)setContentCompressionResistancePriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis
###multipliedBy--设置百分比的
make.width.equalTo(oneBackView.mas_width).multipliedBy(0.5);
###make.baseline.equalTo ---可以理解为两个容器里的两个字控件有位置上的相同点,比如,a容器有一个image,b容器也有一个image,现在想让a容器的image和b容器的image底部相同,就需要用baseline来设置
###priority 设置优先级
setNeedsLayout:告知页面需要更新,但是不会立刻开始更新。执行后会立刻调用layoutSubviews。
layoutIfNeeded:告知页面布局立刻更新。所以一般都会和setNeedsLayout一起使用。如果希望立刻生成新的frame需要调用此方法,利用这点一般布局动画可以在更新布局后直接使用这个方法让动画生效。
layoutSubviews:系统重写布局
setNeedsUpdateConstraints:告知需要更新约束,但是不会立刻开始
updateConstraintsIfNeeded:告知立刻更新约束
updateConstraints:系统更新约束
- (void)updateViewConstraints ViewController的View在更新视图布局时,会先调用ViewController的updateViewConstraints 方法。我们可以通过重写这个方法去更新当前View的内部布局,而不用再继承这个View去重写-updateConstraints方法。我们在重写这个方法时,务必要调用 super 或者 调用当前View的 -updateConstraints 方法。
8,进行布局后如何立马获取控件的高度
[footerView layoutIfNeeded]; // 下面会有关于layoutIfNeeded的介绍
CGFloat tagViewHeight = self.tagView.height;
9,cell上布局的几种类型
一,自定义cell,(cell中 第一个控件或者中间的控件或者最后的控件需要动态隐藏和显示)
二,直接在cell上添加一个view,需要动态隐藏和显示
10,设置控制器为透明
CalendarController* transparentView = [[CalendarController alloc] init];
if ([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) { transparentView.modalPresentationStyle=UIModalPresentationOverCurrentContext;
}else{
self.modalPresentationStyle=UIModalPresentationCurrentContext;
}
transparentView.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
UIViewController* controller = [SettingController rootController];
controller.modalPresentationStyle = UIModalPresentationCurrentContext;
[controller presentViewController:transparentView animated:NO completion:^{///弹出控制器
///控制器出现后调用
}];