使用masonry时遇到的问题和解决方案

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:^{///弹出控制器
        ///控制器出现后调用
    }];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容