Array constraints problem.
jiangqi-coder opened this issue Β· comments
New Issue Checklist
π« If this template is not filled out your issue will be closed with no comment. π«
- I have looked at the Documentation
- I have filled out this issue template.
Issue Info
Info | Value |
---|---|
Platform | iOS |
Platform Version | iPhone X iOS 11.4 |
Masonry Version | 1.1.0 |
Integration Method | cocoapods |
Issue Description
I got a problem!
I used an array that contains some views to make constraints (mas_distributeViewsAlongAxis).
When the device rotate, the subviews need to update constraints. But the subviews is add a new constraint, not update already exist constraint. I found the method
- (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedSpacing:(CGFloat)fixedSpacing leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing
or
- (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedItemLength:(CGFloat)fixedItemLength leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing
used
mas_makeConstraints
to add new constraints. For example, a view already set left = 10.0, and then add a new constraint set left = 15.0, the old left constraints i still exist.
How can i to resolve this problem, i tried to call the method
mas_updateConstraints
in
- (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedSpacing:(CGFloat)fixedSpacing leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing
and it resloved my problem, but i do not think it is a good method to resolve this problem. Please give me a help.
#define UICOLOR_RGB [UIColor colorWithRed:float(256/ 255.0) green:float(256/ 255.0) blue:float(256/ 255.0) alpha:1.0]
#define SCREEN_X (SCREEN_PORTRAIT ? SCREEN_HEIGHT == 812. : SCREEN_WIDTH == 812.)
#define SCREEN_PORTRAIT \
UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)
#define LEFT_RIGHT_EDGE_WIDTH (SCREEN_X ? (SCREEN_PORTRAIT ? 0. : 44.) : 0.)
#define TABBAR_SAFEAREA_HEIGHT (SCREEN_X ? (SCREEN_PORTRAIT ? 34. : 21.) : 0.)
- (instancetype)init {
if (self = [super init]) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
[self createAllSubViews];
}
return self;
}
- (void)createAllSubViews {
UIView *contentView = [[UIView alloc] init];
contentView.backgroundColor = UICOLOR_RGB(0xe8e8e8);
contentView.backgroundColor = [UIColor yellowColor];
[self addSubview:contentView];
self.contentView = contentView;
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.backgroundColor = [UIColor blueColor];
titleLabel.text = @"Share to:";
titleLabel.textColor = [ColorManager color6];
titleLabel.textAlignment = NSTextAlignmentLeft;
titleLabel.font = [UIFont systemFontOfSize:15.0];
[contentView addSubview:titleLabel];
self.titleLabel = titleLabel;
ExclusiveButton *cancelBtn = [[ExclusiveButton alloc] init];
cancelBtn.backgroundColor = [UIColor whiteColor];
cancelBtn.titleLabel.font = [UIFont systemFontOfSize:17.];
[cancelBtn setTitle:@"cancel" forState:UIControlStateNormal];
[cancelBtn setTitleColor:[ColorManager color6] forState:UIControlStateNormal];
[cancelBtn addTarget:self action:@selector(cancelBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cancelBtn cm_makeCorner:5];
[contentView addSubview:cancelBtn];
self.cancelBtn = cancelBtn;
NSArray *imgArray = @[@"ShareToWechatImg.png", @"ShareToWechatZoneImg.png", @"ShareToQQImg.png", @"ShareToQZoneImg.png"];
NSArray *titleArray = @[@"Wechat Friends", @"Wechat Timeline", @"QQ Friends", @"QQ Zone"];
CGFloat margin = 12.5 + LEFT_RIGHT_EDGE_WIDTH;
if (imgArray.count > 0) {
self.viewArray = [NSMutableArray array];
for (NSInteger i = 0; i < imgArray.count; i++) {
UIView *v = [[UIView alloc] init];
v.backgroundColor = [UIColor redColor];
[contentView addSubview:v];
[self.viewArray addObject:v];
ExclusiveButton *btn = [[ExclusiveButton alloc] init];
btn.backgroundColor = [UIColor greenColor];
[btn setImage:[UIImage imageNamed:[imgArray cm_safeStringAtIndex:i]] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(shareBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[v addSubview:btn];
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor cyanColor];
label.text = [titleArray cm_safeStringAtIndex:i];
label.textColor = [ColorManager color6];
label.font = [UIFont systemFontOfSize:13];
label.textAlignment = NSTextAlignmentCenter;
[v addSubview:label];
[btn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(0.);
make.centerX.equalTo(v);
make.size.mas_equalTo(CGSizeMake(50., 50.));
}];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(btn.mas_bottom).mas_offset(10.);
make.left.right.mas_equalTo(0.);
make.height.mas_equalTo(20.);
}];
}
[self.viewArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:15. leadSpacing:margin tailSpacing:margin];
[self.viewArray mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(titleLabel.mas_bottom).mas_offset(15.);
make.height.mas_equalTo(80.);
}];
}
else {
UILabel *unSupportLabel = [[UILabel alloc] init];
unSupportLabel.text = @"Your device is not support share to Wechat or QQ, please to install the Wechat or QQ client on AppStore.";
unSupportLabel.textColor = [ColorManager color6];
unSupportLabel.font = [UIFont systemFontOfSize:14];
unSupportLabel.textAlignment = NSTextAlignmentCenter;
unSupportLabel.numberOfLines = 0;
unSupportLabel.lineBreakMode = NSLineBreakByWordWrapping;
[contentView addSubview:unSupportLabel];
self.unSupportLabel = unSupportLabel;
[unSupportLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(titleLabel.mas_bottom).mas_offset(15.);
make.left.right.equalTo(titleLabel);
make.height.mas_equalTo(80.);
}];
}
[contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.bottom.right.mas_equalTo(0.);
}];
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(15.);
make.left.mas_equalTo(margin);
make.right.mas_equalTo(-margin);
make.height.mas_equalTo(20.);
}];
[cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(titleLabel.mas_bottom).mas_offset(80. + 30.);
make.left.right.equalTo(self.titleLabel);
make.bottom.mas_equalTo(contentView.mas_bottom).mas_offset(-(15. + TABBAR_SAFEAREA_HEIGHT));
make.height.mas_equalTo(40.);
}];
}
- (void)shareBtnClicked:(UIButton *)sender {
}
- (void)cancelBtnClicked:(UIButton *)sender {
[self removeFromSuperview];
}
- (void)orientationChanged:(NSNotification *)noti {
CGFloat margin = 12.5 + LEFT_RIGHT_EDGE_WIDTH;
[self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(margin);
make.right.mas_equalTo(-margin);
}];
[self.viewArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:15. leadSpacing:margin tailSpacing:margin];
[self.cancelBtn mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self.contentView.mas_bottom).mas_offset(-(15. + TABBAR_SAFEAREA_HEIGHT));
}];
}