iOS原生的布局写法
方法1:使用 HV方式
self.backImageView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[backimage]-|" options:NSLayoutFormatAlignmentMask metrics:nil views:@{@"backimage":self.backImageView}]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[backimage]-|" options:NSLayoutFormatAlignmentMask metrics:nil views:@{@"backimage":self.backImageView}]];
//直接加
self.phoneView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.phoneView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.logoImage attribute:NSLayoutAttributeBottom multiplier:1.0 constant:33 * FitHeight]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.phoneView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.phoneView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0 constant:230 * FitWidth]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.phoneView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:42 * FitHeight]];
采用 masorny 的写法
//电话框
[self.phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.logoImage.mas_bottom).offset(33 * FitHeight);
make.centerX.equalTo(self.view.mas_centerX);
make.width.equalTo(@(230 * FitWidth));
make.height.equalTo(@(42 * FitHeight));
}];
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步