iOS开发Masonry与Frame布局差异
示例
#import "ViewController.h" #import <Masonry/Masonry.h> @interface ViewController () @property (nonatomic, strong)UIView *view1; @property (nonatomic, strong)UIView *view2; @property (nonatomic, strong)UIView *view3; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self.view addSubview:self.view1]; [self.view addSubview:self.view2]; [self.view addSubview:self.view3]; [self.view1 mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(14.272189); make.right.mas_equalTo(-14.272189); make.top.mas_equalTo(50); make.height.mas_equalTo(89.795858); }]; [self.view2 mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(30); make.top.mas_equalTo(200); make.size.mas_equalTo(CGSizeMake(172.455621, 89.795858)); }]; self.view3.frame = CGRectMake(30, 400, 172.455621, 89.795858); } - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; NSLog(@"view1:%lf,%lf,%lf,%lf",self.view1.frame.origin.x,self.view1.frame.size.width,self.view1.frame.size.height,self.view.frame.size.width-(14.272189*2)); NSLog(@"view2:%lf,%lf",self.view2.frame.size.width,self.view2.frame.size.height); NSLog(@"view3:%lf,%lf",self.view3.frame.size.width,self.view3.frame.size.height); } #pragma mark - - (UIView *)view1 { if (!_view1) { _view1 = [[UIView alloc] init]; _view1.backgroundColor = [UIColor redColor]; } return _view1; } - (UIView *)view2 { if (!_view2) { _view2 = [[UIView alloc] init]; _view2.backgroundColor = [UIColor blueColor]; } return _view2; } - (UIView *)view3 { if (!_view3) { _view3 = [[UIView alloc] init]; _view3.backgroundColor = [UIColor yellowColor]; } return _view3; } @end
结果
iOS14.1 iPhone6s
view1:14.500000,346.000000,90.000000,346.455622
view2:172.500000,90.000000
view3:172.455621,89.795858iOS16.4 iPhone14
view1:14.333333,361.333333,89.666667,361.455622
view2:172.333333,89.666667
view3:172.455621,89.795858
自动布局时系统会根据屏幕分辨率将传入的数值转换为最接近屏幕分辨率展示的大小。
如:
view1中传入的左右大小为14.272189,在iPhone6s(2x)中最接近能展示的14.5,故自动布局后宽为346.0(375.0-14.5-14.5);在iPhone14(3x)中最接近能展示的14.33333,故自动布局后宽为361.333333(390.0-14.33333-14.33333)。
view2宽度在iPhone14(3x)中最接近能展示的172.333333(与172.333333相差0.122288,与172.666667相差0.211046)。
注意:如UILabel未设置其具体高度,Masonry会根据其展示需要的宽高(可通过boundingRectWithSize...方法获取)及屏幕分辨率向上适配至最小宽高。
以上就是iOS开发Masonry与Frame布局差异示例详解的详细内容,更多关于iOS Masonry Frame布局差异的资料请关注好代码网其它相关文章!