美文网首页
iOS10以下 在layoutSubviews中修改本身的Fra

iOS10以下 在layoutSubviews中修改本身的Fra

作者: JohnayXiao | 来源:发表于2018-03-01 18:00 被阅读366次

    经过多次测试,只有10.0以上版本才能修改Frame 不然会崩溃

    - (void)layoutSubviews
    {
        [super layoutSubviews];
    
    //    if(kDevice_System >= 10.0) {
    //
    //        NSString *content = self.titleLabel.text;
    //        UIFont *font = self.titleLabel.font;
    //        CGSize size = CGSizeMake(MAXFLOAT, self.height);
    //        CGSize buttonSize = [content boundingRectWithSize:size
    //                                                  options:NSStringDrawingTruncatesLastVisibleLine  | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
    //                                               attributes:@{ NSFontAttributeName:font}
    //                                                  context:nil].size;
    //
    //        self.width = buttonSize.width + FitValue(70);
    //
    //    }
    
        // 调整图片
        self.imageView.x = FitValue(5);
        self.imageView.y = FitValue(10);
        self.imageView.width = FitValue(20);
        self.imageView.height = self.imageView.width;
    
        // 调整文字
        self.titleLabel.x = self.imageView.width + FitValue(15);
        self.titleLabel.y = 0;
        self.titleLabel.width = self.width - self.imageView.width - FitValue(10);
        self.titleLabel.height = self.height;
        
        
    }
    

    不能修改,那就直接更新约束,放在重写title方法中,更节能

    - (void)setTitle:(NSString *)title forState:(UIControlState)state {
        
        [super setTitle:title forState:state];
        
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            
            UIFont *font = self.titleLabel.font;
            CGSize size = CGSizeMake(MAXFLOAT, self.height);
            CGSize buttonSize = [title boundingRectWithSize:size
                                                      options:NSStringDrawingTruncatesLastVisibleLine  | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                                   attributes:@{ NSFontAttributeName:font}
                                                      context:nil].size;
            
            make.width.equalTo(@(buttonSize.width + FitValue(60)));
        }];
    }
    
    

    相关文章

      网友评论

          本文标题:iOS10以下 在layoutSubviews中修改本身的Fra

          本文链接:https://www.haomeiwen.com/subject/ftrnxftx.html