使用AutoLayout布局适配时,提前获得AutoLayout完成适配后的子控件的真实frame

  • 遇到的坑留个笔记
  • 项目中用Masonry布局,有地方需要用到frame,不管怎么取值都是0,坑死我啦

- (void)initTopView{

    _cancelButton = ({
        UIButton *button = [UIButton buttonWithTitle:@"取消" titleColor:[UIColor blackColor] fontSize:14 target:self action:@selector(cancelButtonClick:)];
        [self.view addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_searchBar);
            make.right.equalTo(self.view.mas_right).offset(-5);
        }];
        
        button;
    });
    _cancelButton.hidden = YES;
    
    
    _topView = ({
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_searchBar.mas_bottom).offset(5);
            make.left.right.equalTo(self.view);
            make.height.equalTo(self.view).multipliedBy(0.2);
        }];
        
        view;
    });
    
    _consultButton = ({
        UIButton *button = [UIButton buttonWithTopAndBottomTitle:@"咨询记录" titleColor:[UIColor blackColor] fontSize:14 imageName:@"publish-text" selectedImageName:@"publish-text" target:self action:@selector(consultButtonClick:)];
        [self.topView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_topView).offset(-13);
            make.centerX.equalTo(_topView);
        }];
        
        button;
    });
    
    _doctorButton = ({
        UIButton *button = [UIButton buttonWithTopAndBottomTitle:@"问医生" titleColor:[UIColor blackColor] fontSize:13 imageName:@"publish-audio" selectedImageName:@"publish-text" target:self action:@selector(doctortButtonClick:)];
        [self.view addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_consultButton.mas_top);
            make.centerX.equalTo(_topView).multipliedBy(0.3);
        }];
        
        button;
    });
    
    _ideaButton = ({
        UIButton *button = [UIButton buttonWithTopAndBottomTitle:@"意见反馈" titleColor:[UIColor blackColor] fontSize:13 imageName:@"publish-picture" selectedImageName:@"publish-text" target:self action:@selector(doctortButtonClick:)];
        [self.view addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_consultButton.mas_top);
            make.centerX.equalTo(_topView).multipliedBy(1.7);
        }];
        
        button;
    });
    
}
  • 不加这下面两句,获得的尺寸会是你使用Masonry布局 或(xib)里的未完成autolayout适配时的尺寸

  • storyboard同理(把这两句写在viewDidLoad:方法中,将contentView换成控制器的view)
    [self.contentView setNeedsLayout];
    [self.contentView layoutIfNeeded];
  • 这里可以提前获得autolayout完成后适配后的子控件的真实frame
    self.imageView.layer.cornerRadius = self.imageView.frame.size.width / 2;
    self.imageView.clipsToBounds = YES;

你可能感兴趣的:(使用AutoLayout布局适配时,提前获得AutoLayout完成适配后的子控件的真实frame)