initFrame 和 masonry 同时使用,initFrame无效,一定要在masonry内设置元素的width和height。否则它的长宽就会由其内部元素的masonry决定,而不是由initFrame时设定的长宽决定。
举例:
- (void)configLastLoginViewWithButton:(UIButton *)lastLoginButton{
[self.view layoutIfNeeded];
ApplicationSettings *settings = [ApplicationSettings sharedInstance];
UIView *lastLoginImageView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, lastLoginImageWidth, lastLoginImageHeight)];
lastLoginImageView.backgroundColor = [[UIElementManager sharedInstance] toastColor];
lastLoginImageView.layer.shadowColor = [kMajorColor CGColor];
lastLoginImageView.layer.shadowOpacity = 0.18; // 0.0 ~ 1.0 的值
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, lastLoginImageWidth, lastLoginImageHeight)
byRoundingCorners:UIRectCornerAllCorners
cornerRadii:CGSizeMake(6.0f, 6.0f)];
lastLoginImageView.layer.shadowPath = bezierPath.CGPath;
lastLoginImageView.layer.cornerRadius = 5.0f;
lastLoginImageView.layer.masksToBounds = NO;
lastLoginImageView.layer.cornerRadius = 6.0f;
if (self.isTriangleViewRotate) {
lastLoginImageView.layer.shadowOffset = CGSizeMake(0, -1); //[水平偏移, 垂直偏移]
} else {
lastLoginImageView.layer.shadowOffset = CGSizeMake(0, 1); //[水平偏移, 垂直偏移]
}
UILabel *lastLoginTitleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
lastLoginTitleLabel.font = [[UIElementManager sharedInstance] .currentTheme yn_lFontOfSize:11.0f];
lastLoginTitleLabel.textAlignment = NSTextAlignmentCenter;
lastLoginTitleLabel.textColor = [YNThemeCommonUtils text2Color];
lastLoginTitleLabel.text = UI_STRING(@"lastLoginUser");
[lastLoginImageView addSubview:lastLoginTitleLabel];
[lastLoginTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(lastLoginImageView);
make.top.greaterThanOrEqualTo(lastLoginImageView).offset(8);
}];
UILabel *lastLoginValueLabel = [[UILabel alloc] initWithFrame:CGRectZero];
lastLoginValueLabel.numberOfLines = 1;
lastLoginValueLabel.font = [[UIElementManager sharedInstance] .currentTheme yn_rFontOfSize:15.0f];
lastLoginValueLabel.textAlignment = NSTextAlignmentCenter;
lastLoginValueLabel.textColor = [[UIElementManager sharedInstance] header1Color];
lastLoginValueLabel.text = settings.lastLoginUser;
lastLoginValueLabel.lineBreakMode = NSLineBreakByTruncatingTail;
[lastLoginImageView addSubview:lastLoginValueLabel];
[lastLoginValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(lastLoginTitleLabel);
make.top.equalTo(lastLoginTitleLabel.mas_bottom).offset(2);
make.left.greaterThanOrEqualTo(lastLoginImageView).offset(10);
make.right.lessThanOrEqualTo(lastLoginImageView).offset(-10);
make.bottom.mas_equalTo(lastLoginImageView).mas_offset(-9);
}];
[self.scrollView addSubview:lastLoginImageView];
UIImageView *lastLoginTriangleView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"triangle"] imageWithTintColor:[[UIElementManager sharedInstance] toastColor]]];
if (self.isTriangleViewRotate) {
lastLoginTriangleView.transform = CGAffineTransformRotate(lastLoginTriangleView.transform, M_PI * -1);
}
[self.scrollView addSubview:lastLoginTriangleView];
lastLoginImageView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
lastLoginTriangleView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
[lastLoginTriangleView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(lastLoginButton);
if (self.isTriangleViewRotate) {
make.top.equalTo(lastLoginButton.mas_bottom).offset(-3);
} else {
make.bottom.equalTo(lastLoginButton.mas_top).offset(3);
}
}];
CGRect lastFrame = [lastLoginButton.superview convertRect:lastLoginButton.frame toView:self.scrollView];
[lastLoginImageView mas_makeConstraints:^(MASConstraintMaker *make) {
if (self.isTriangleViewRotate) {
make.top.equalTo(lastLoginTriangleView.mas_bottom);
} else {
make.bottom.equalTo(lastLoginTriangleView.mas_top);
}
make.height.mas_equalTo(lastLoginImageHeight);
make.width.mas_equalTo(lastLoginImageWidth);
if (lastFrame.origin.x + lastFrame.size.width * 0.5f < lastLoginImageWidth * 0.5f + 25) {
make.left.mas_equalTo(25);
} else if (self.scrollView.width - (lastFrame.origin.x + lastFrame.size.width * 0.5f) < lastLoginImageWidth * 0.5f + 25){
make.right.mas_offset(-25);
} else {
make.centerX.equalTo(lastLoginButton);
}
}];
}
这里的lastLoginImageView在初始化的时候设置的长宽是无效的,如果masonry内没有设置height,那么它的高度会由lastLoginTitleLabel和lastLoginValueLabel的masonry决定。
网友评论