魔性小胖 發表於 2023-11-9 11:17:27

iOS开发Masonry与Frame布局差异示例详解

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>&nbsp;iOS开发Masonry与Frame布局差异</li><ul class="second_class_ul"><li>示例</li><li>结果</li></ul></ul></div><p class="maodian"></p><h2>&nbsp;iOS开发Masonry与Frame布局差异</h2>
<p class="maodian"></p><h3>示例</h3>
<div class="jb51code"><pre class="brush:cpp;">#import "ViewController.h"
#import &lt;Masonry/Masonry.h&gt;
@interface ViewController ()
@property (nonatomic, strong)UIView *view1;
@property (nonatomic, strong)UIView *view2;
@property (nonatomic, strong)UIView *view3;
@end
@implementation ViewController
- (void)viewDidLoad {
    ;
    // Do any additional setup after loading the view.
    ;
    ;
    ;
    [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 {
    ;
    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 = [ init];
      _view1.backgroundColor = ;
    }
    return _view1;
}
- (UIView *)view2 {
    if (!_view2) {
      _view2 = [ init];
      _view2.backgroundColor = ;
    }
    return _view2;
}
- (UIView *)view3 {
    if (!_view3) {
      _view3 = [ init];
      _view3.backgroundColor = ;
    }
    return _view3;
}
@end</pre></div>
<p class="maodian"></p><h3>结果</h3>
<blockquote><p>iOS14.1 iPhone6s<br />view1:14.500000,346.000000,90.000000,346.455622<br />view2:172.500000,90.000000<br />view3:172.455621,89.795858</p>
<p>iOS16.4 iPhone14<br />view1:14.333333,361.333333,89.666667,361.455622<br />view2:172.333333,89.666667<br />view3:172.455621,89.795858</p></blockquote>
<p><strong>自动布局时系统会根据屏幕分辨率将传入的数值转换为最接近屏幕分辨率展示的大小</strong>。</p>
<p>如:</p>
<p>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)。</p>
<p>view2宽度在iPhone14(3x)中最接近能展示的172.333333(与172.333333相差0.122288,与172.666667相差0.211046)。</p>
<p><strong>注意:如UILabel未设置其具体高度,Masonry会根据其展示需要的宽高(可通过boundingRectWithSize...方法获取)及屏幕分辨率向上适配至最小宽高</strong>。</p>
<p>以上就是iOS开发Masonry与Frame布局差异示例详解的详细内容,更多关于iOS Masonry Frame布局差异的资料请关注琼殿技术社区其它相关文章!</p>
                           
                            <div class="art_xg">
                              <b>您可能感兴趣的文章:</b><ul><li>iOS界面布局简化UIStackView使用详解</li><li>iOS布局渲染之UIView方法的调用时机详解</li><li>详解iOS自定义UITabBar与布局</li><li>iOS ScrollView实现自动布局的方法(适用Swift 3.0 )</li><li>深入理解IOS控件布局之Masonry布局框架</li><li>IOS xib布局小技巧-边框设置</li></ul>
                            </div>

                        </div>
                        <!--endmain-->
頁: [1]
查看完整版本: iOS开发Masonry与Frame布局差异示例详解