Masory相关
2018-06-18 本文已影响0人
愤怒小鸟飞呀飞
问题:当a 相对于B设定约束关系,B隐藏后,a会怎么展示?
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor greenColor];
[self.view addSubview:button];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button2.backgroundColor = [UIColor redColor];
[button2 setTitle:@"push" forState:UIControlStateNormal];
[self.view addSubview:button2];
[button2 addTarget:self action:@selector(button2Action:) forControlEvents:UIControlEventTouchUpInside];
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.view);
make.width.height.equalTo(@100);
make.top.equalTo(self.view).offset(100);
}];
[button2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.view);
make.width.height.equalTo(@100);
make.top.equalTo(button).offset(100);
}];
// button.hidden = YES;
结果如下:
image.png image.png
问题二:
A,B两个视图,A隐藏之后,B移动到A的位置,怎么实现?
image.png如上图所示,@消失后,表情符号挪动到@的位置
答案:设置@的宽度为0,刷新视图
代码
- (void)buttonAction:(UIButton *)btn{
// TwoViewController *two = [[TwoViewController alloc] init];
// [self presentViewController:two animated:YES completion:^{
//
// }];
//
//发送请求
[_button mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(@0);
}];
[self.view setNeedsDisplay];
[self.view layoutIfNeeded];
}
问题三
如下图:2不存在了,3替换2的位置,这种需求用masory能实现么?
46021532949786_.pic.jpg
答案:更新2的top和height
- (void) initSubViews{
UIView *contentView = [[UIView alloc] init];
[self addSubview:contentView];
[contentView addSubview:self.blankImageView];
[contentView addSubview:self.refreshBtn];
[contentView addSubview:self.msgTitleLabel];
[contentView addSubview:self.msgInfoLabel];
[contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.blankImageView.mas_top);
make.center.equalTo(self);
make.bottom.equalTo(self.refreshBtn.mas_bottom);
make.left.equalTo(@58);
make.right.equalTo(@-58);
}];
[self.blankImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(contentView.mas_top);
make.width.height.equalTo(@119);
make.centerX.equalTo(self);
}];
[self.msgTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.blankImageView.mas_bottom).offset(8);
make.centerX.equalTo(self);
make.left.right.equalTo(contentView);
}];
[self.msgInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.top.equalTo(self.msgTitleLabel.mas_bottom).offset(8);
make.left.right.equalTo(contentView);
}];
[self.refreshBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.msgInfoLabel.mas_bottom).offset(27);
make.centerX.equalTo(self);
make.height.equalTo(@57);
make.bottom.equalTo(contentView.mas_bottom);
make.left.right.equalTo(contentView);
}];
if (!self.messageInfo.length) {
[self.msgInfoLabel mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.msgTitleLabel.mas_bottom);
make.height.equalTo(@0);
}];
}
}
4、绕进一个死胡同
51641533899101_.pic_hd.jpg
如上图,想让label没有内容时,高度为0,有内容时,自适应,结果写出来了上面的代码,还是理解不深,都删掉,一个行代码都不用写,本来就是自适应的,label.text = nil,就可以了
5、API 学习
- (UILayoutPriority)contentHuggingPriorityForAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);
- (void)setContentHuggingPriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);
- (UILayoutPriority)contentCompressionResistancePriorityForAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);
- (void)setContentCompressionResistancePriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);
setContentHuggingPriority
当视图实际大小大于设置约束大小时,设置视图变化策略
setContentCompressionResistancePriority
当视图实际大小小于设置约束大小时,设置视图变化策略
使用场景:狐友myHome页面 userInfo 昵称 性别 二维码展示
参考链接 https://blog.csdn.net/hherima/article/details/53318306
6、关于Label自适应高度和宽度问题
image.png
如上图所示红色部分,图片高度随content高度改变,使用Masonry如何写约束呢?
//feedBackBtn
negativeFeedback.snp.makeConstraints { (maker) in
maker.width.height.equalTo(15)
maker.right.equalTo(self).offset(-kMargin_14)
maker.centerY.equalTo(avator)
}
//content
content.snp.makeConstraints { (maker) in
maker.left.equalTo(avator)
maker.top.equalTo(avator.snp.bottom).offset(kMargin_10)
maker.height.equalTo(22)
maker.right.equalTo(self).offset(-kMargin_14)
maker.bottom.equalTo(self.middleView.snp.top).offset(-kMargin_10)
}
content.setContentCompressionResistancePriority(.defaultHigh, for: .vertical)
let bubbleObj = BubblesModel.init()
bubbleObj.actionKey = COPY_SEL_KEY
bubbleObj.title = "复制";
content.bubblesActions = [bubbleObj]
content.canShowBubbles = true;
//middleView
middleView.snp.makeConstraints { (maker) in
maker.left.right.equalTo(content)
maker.height.equalTo(TLContainerConstants.middleViewHeight)
maker.bottom.equalTo(funcName.snp.top).offset(-10)
}
……
if self.viewModel_s?.model?.funcCont?.content?.count == 0 {
//功能容器内容不存在
content.snp.updateConstraints { (maker) in
maker.height.equalTo(viewModel_s?.contentViewHeight ?? 0)
maker.top.equalTo(avator.snp.bottom).offset(0)
}
}else{
//功能容器内容存在
content.snp.updateConstraints { (maker) in
maker.height.equalTo(viewModel_s?.contentViewHeight ?? 0)
maker.top.equalTo(avator.snp.bottom).offset(kMargin_10)
}
}
如果把高度约束注释,更新高度去掉
image.png
,运行效果如下
image.png
内容不能正常显示。。。
在使用Label 作为Layout高度方程式高度计算时,需要手动设置高度
在label不作为高度计算是,则不需要手动设置,如下代码
//contentBgGrayView
cotentBgGrayView.snp.makeConstraints { (maker) in
maker.left.equalTo(self.snp.left).offset(kMargin_14)
maker.right.equalTo(self.snp.right).offset(-kMargin_14)
maker.bottom.equalTo(enterBtn.snp.bottom).offset(kMargin_14)
maker.top.equalTo(enterBtn.snp.top).offset(-kMargin_14)
}
//content
content.snp.makeConstraints { (maker) in
maker.left.equalTo(self.snp.left).offset(24)
maker.centerY.equalTo(enterBtn)
maker.right.lessThanOrEqualTo(enterBtn.snp.left).offset(-20)
}
content.setContentCompressionResistancePriority(UILayoutPriority.defaultLow, for: NSLayoutConstraint.Axis.horizontal)
//enterButton
enterBtn.snp.makeConstraints { (maker) in
maker.top.equalTo(middleView.snp.bottom).offset(kMargin_14)
maker.height.equalTo(32)
maker.width.equalTo(58)
maker.right.equalTo(cotentBgGrayView.snp.right).offset(-kMargin_10)
}
垂直方向的方程式计算,直接用button与空间间隔计算即可
7、关于控制台 打印警告信息(虽然布局已经按照预想布置)
2019-04-03 16:06:12.186477+0800 sohuhy[73454:6682617] [LayoutConstraints] 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.
(
"<SnapKit.LayoutConstraint:0x280ec8b40@TLContainerCell.swift#100 UIView:0x105d4a630.bottom == sohuhy.TLContainerNoAvatorVideoCell:0x1061df600.bottom>",
"<SnapKit.LayoutConstraint:0x280ecbc60@TLContainerNoAvatorCell.swift#60 UILabel:0x112c1fc20.height == 12.0>",
"<SnapKit.LayoutConstraint:0x280ecbba0@TLContainerNoAvatorCell.swift#61 UILabel:0x112c1fc20.top == sohuhy.TLContainerNoAvatorVideoCell:0x1061df600.top + 10.0>",
"<SnapKit.LayoutConstraint:0x280e802a0@TLContainerNoAvatorCell.swift#77 TLFeedVideoView:0x105d8c3c0.height == 386.0>",
"<SnapKit.LayoutConstraint:0x280e80300@TLContainerNoAvatorCell.swift#78 TLFeedVideoView:0x105d8c3c0.top == UILabel:0x112c1fc20.bottom + 10.0>",
"<SnapKit.LayoutConstraint:0x280e80f60@TLContainerNoAvatorCell.swift#104 SNSBaseButton:0x105d8c5b0.top == TLFeedVideoView:0x105d8c3c0.bottom + 14.0>",
"<SnapKit.LayoutConstraint:0x280e80fc0@TLContainerNoAvatorCell.swift#105 SNSBaseButton:0x105d8c5b0.height == 32.0>",
"<SnapKit.LayoutConstraint:0x280e810e0@TLContainerNoAvatorCell.swift#108 SNSBaseButton:0x105d8c5b0.bottom == UIView:0x105d4a630.bottom + 14.0>",
"<NSLayoutConstraint:0x28098bc50 sohuhy.TLContainerNoAvatorVideoCell:0x1061df600.height == 517>"
)
Will attempt to recover by breaking constraint
<SnapKit.LayoutConstraint:0x280e802a0@TLContainerNoAvatorCell.swift#77 TLFeedVideoView:0x105d8c3c0.height == 386.0>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
SnsVideoPlayerView.m:621 hzq!!!_timeLineView_stopVideo
如上所述,依照log提示,慢慢计算,一定是哪里的约束给重了,给了两次,在一定情况下造成冲突