initFrame 和 masonry 同时使用

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决定。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,865评论 1 92
  • •前端面试题汇总 一、HTML和CSS 21 你做的页面在哪些流览器测试过?这些浏览器的内核分别是什么? ...
    Simon_s阅读 2,241评论 0 8
  • 一:在制作一个Web应用或Web站点的过程中,你是如何考虑他的UI、安全性、高性能、SEO、可维护性以及技术因素的...
    Arno_z阅读 1,277评论 0 1
  • 因为绿萝属于常绿植物,而且是最好的吸收废气的植物,很多朋友在家居生活甚至办公场所养了绿萝。但很多的养殖者却不是很清...
    多肉花客阅读 687评论 0 0
  • 厚石板,青石桥,弯弯曲曲的小道,青灰小筑亭台楼阁,素色烟雨,最美不过江南。 想去江南由来已久,烟雨拂柳一度是我心中...
    北城未眠阅读 1,120评论 19 44