美文网首页iOS开发之笔记摘录
布局 frame autoresizing autoLayout

布局 frame autoresizing autoLayout

作者: 平安喜乐698 | 来源:发表于2017-07-14 17:51 被阅读321490次
目录

  1.布局方式
    frame
    autoresizing
    autolayout
    sizeClass
  2.三方库
    Masonry
    UIView+AutoLayout
  3.适配
    图片适配
1.布局方式
    1.frame
        不适用,无法适配多种机型
    2.autoResizing
    3.autoLayout
    4.sizeClass
1.1 frame(x,y,width,height)
    固定的位置和大小
        大小固定(不随父视图改变)
        位置相对父视图固定(随父视图移动,相对父视图不移动)

例:
    [[UILabel new] setFrame:CGRectMake(0, 0, 100, 35)];
1.2. autoresizing
只局限于约束视图相对于父视图

    // 对父视图设置autoresizesSubviews:true(默认)时,其子view会根据自已的autoresizingMask属性值自动调整与父视图的位置和大小.
    self.view.autoresizesSubviews=true;

    // 对子视图设置autoresizingMask
    // 父视图大小发生变化时,子视图根据autoresizingMask属性变换自己
    // 自动调整与父视图的下边距
    view.autoresizingMask=UIViewAutoresizing.FlexibleBottomMargin;      
    /*
     6种(默认值是.None)( 多种组合使用[]数组)
     .FlexibleBottomMargin
     .FlexibleTopMargin
     .FlexibleLeftMargin
     .FlexibleRightMargin
     .FlexibleHeight
     .FlexibleWidth
     */
注意:
    在使用xib,storyboard时要取消勾选Autolayout(默认自动勾选autolayout)(Autoresizing与Autolayout相互冲突)
1.3. autolayout
       (受欢迎的三方库:Masonry、SDAutoLayout)

        不局限于约束视图相对于父视图,可以是任意两个视图(要有公共父视图),公共约束被添加在最小公视图上。
        约束可设置优先级(0~1000,越大级别越高)
        要修改通过约束布局的控件的布局时(高度适配时需动),可将此约束作为属性,修改其constaints
        自动布局时动画要加上 [self layoutifneeded],否则可能无效。
2种方式:
    1. 代码
    2. xib

1.3.1. 代码

        // 1.禁止对直接子视图自动缩放(默认:true)
        [self.view setAutoresizesSubviews:false];
        // 2.子视图禁用自动缩放,使用约束(每一个view必须加,默认true)             
        UIView *view=[UIView new];
        [view setTranslatesAutoresizingMaskIntoConstraints:false];
        [self.view addSubview:view];


addConstraint
addConstraints
removeConstraint
removeConstraints



创建方式一
    [2控件作布局]
        // 宽1:4         (视图,宽,等于,nil或视图2,宽,比例,相差)
        self.view.addConstraint(NSLayoutConstraint.init(item: view, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 0.25, constant: 0))           // 3.对两个视图添加约束(在父视图上加)
                    // .Left  .Right  .Bottom  .Top  .Width  .Height  .Leading  .Trailing   .CenterX  .CenterY   .Baseline                  // .LessThanOrEqual==-1  .GreaterThanOrEqual==1   .Equal==0

              tra.constant=10                   // 值
              self.view.layoutIfNeeded()        // 重新布局
              tra.firstItem
              tra.firstAttribute   tra.secondAttribute
              tra.identifier
              button.constraints enum遍历
              tra.active = YES;   // 是否有效,设置为NO后无效必须重建
              
    [单独控件作布局]
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:bottomView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.f constant:BOTTOM_BUTTON_HEIGHT]];



创建方式二       使用正则表达式 添加多个约束

            var arr=[NSLayoutConstraint]()
            // 约束("",固定无意义,nil,[])
            arr+=NSLayoutConstraint.constraintsWithVisualFormat("[backButton(>=200)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: ["backButton":backButton])
            self.view.addConstraints(arr)

                H:|-50-[redView(100)]-50-[blueView(==redView)]
                H 横向    | 屏幕边缘   -相距-    [控件(h时是高 V时是宽 ==和另一个控件相等)]


    // H:横向  V:纵向  [view1]:控件  |:父视图  -:间隔 (与父==20 与它==8)  ():-(数值)- view(<=view2)
    // @:@1000优先级最大
    // 不能使用[self.view]
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H|-(padding)-[_view1]|" options:NSLayoutFormatAlignAllLeft metrics:@{@"padding":@(100)} views:view1]];
    //    NSDictionaryOfVariableBindings(view1)
    
    
[例:横向居中
    NSDictionary *views = @{@"mainTableView":self.mainTableView, @"bottomView":bottomView};
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[mainTableView]|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:views]];
]

NSLayoutConstraint : NSObject

添加约束(在公共父视图上)
    // 方式一
    // 单个控件(conView的高等于100的1倍)
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:conView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.f constant:100]];
    // 2个控件(conView的上等于conView2的上+100的1倍)
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:conView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:conView2 attribute:NSLayoutAttributeTop multiplier:1.f constant:100]];
    // 方式二(conView踞左100)
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H|-(padding)-[view1]|" options:NSLayoutFormatAlignAllLeft metrics:@{@"padding":@(100)} views:@{@"view1":conView}]];
    
    /*
     attribute:
     
     NSLayoutAttributeLeft = 1,
     NSLayoutAttributeRight,
     NSLayoutAttributeTop,
     NSLayoutAttributeBottom,
     NSLayoutAttributeLeading,
     NSLayoutAttributeTrailing,
     NSLayoutAttributeWidth,
     NSLayoutAttributeHeight,
     NSLayoutAttributeCenterX,
     NSLayoutAttributeCenterY,
     NSLayoutAttributeLastBaseline,
     NSLayoutAttributeBaseline NS_SWIFT_UNAVAILABLE("Use 'lastBaseline' instead") = NSLayoutAttributeLastBaseline,
     NSLayoutAttributeFirstBaseline NS_ENUM_AVAILABLE_IOS(8_0),
     
     
     NSLayoutAttributeLeftMargin NS_ENUM_AVAILABLE_IOS(8_0),
     NSLayoutAttributeRightMargin NS_ENUM_AVAILABLE_IOS(8_0),
     NSLayoutAttributeTopMargin NS_ENUM_AVAILABLE_IOS(8_0),
     NSLayoutAttributeBottomMargin NS_ENUM_AVAILABLE_IOS(8_0),
     NSLayoutAttributeLeadingMargin NS_ENUM_AVAILABLE_IOS(8_0),
     NSLayoutAttributeTrailingMargin NS_ENUM_AVAILABLE_IOS(8_0),
     NSLayoutAttributeCenterXWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),
     NSLayoutAttributeCenterYWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),
     
     NSLayoutAttributeNotAnAttribute = 0
     */
    
    /*
     relatedBy:
     
     NSLayoutRelationLessThanOrEqual = -1,
     NSLayoutRelationEqual = 0,
     NSLayoutRelationGreaterThanOrEqual = 1,
     */
    
    /*
     options:
     
     NSLayoutFormatAlignAllLeft = (1 << NSLayoutAttributeLeft),
     NSLayoutFormatAlignAllRight = (1 << NSLayoutAttributeRight),
     NSLayoutFormatAlignAllTop = (1 << NSLayoutAttributeTop),
     NSLayoutFormatAlignAllBottom = (1 << NSLayoutAttributeBottom),
     NSLayoutFormatAlignAllLeading = (1 << NSLayoutAttributeLeading),
     NSLayoutFormatAlignAllTrailing = (1 << NSLayoutAttributeTrailing),
     NSLayoutFormatAlignAllCenterX = (1 << NSLayoutAttributeCenterX),
     NSLayoutFormatAlignAllCenterY = (1 << NSLayoutAttributeCenterY),
     NSLayoutFormatAlignAllLastBaseline = (1 << NSLayoutAttributeLastBaseline),
     NSLayoutFormatAlignAllBaseline NS_SWIFT_UNAVAILABLE("Use 'alignAllLastBaseline' instead") = NSLayoutFormatAlignAllLastBaseline,
     NSLayoutFormatAlignAllFirstBaseline NS_ENUM_AVAILABLE_IOS(8_0) = (1 << NSLayoutAttributeFirstBaseline),
     
     NSLayoutFormatAlignmentMask = 0xFFFF,
     
    NSLayoutFormatDirectionLeadingToTrailing = 0 << 16, // default
    NSLayoutFormatDirectionLeftToRight = 1 << 16,
    NSLayoutFormatDirectionRightToLeft = 2 << 16,
    
    NSLayoutFormatDirectionMask = 0x3 << 16,
    
    NSLayoutFormatSpacingEdgeToEdge API_AVAILABLE(ios(11.0),tvos(11.0)) = 0 << 19, // default
     
    NSLayoutFormatSpacingBaselineToBaseline API_AVAILABLE(ios(11.0),tvos(11.0)) = 1 << 19,
    NSLayoutFormatSpacingMask API_AVAILABLE(ios(11.0),tvos(11.0)) = 0x1 << 19,

     */


删除约束
    [self.view removeConstraint:cons1];
    [self.view removeConstraints:@[]];
属性
    // 约束值
    @property CGFloat constant;
    // 优先级
    @property UILayoutPriority priority;
    /*
     UILayoutPriorityRequired           必须满足        1000(必选约束)
     UILayoutPriorityDefaultHigh        按钮防压缩       750(可选约束,会优先满足必选约束)
     UILayoutPriorityDefaultLow         按钮仅内容宽      250(可选约束,会优先满足必选约束)
     UILayoutPriorityFittingSizeLevel                   50(可选约束,会优先满足必选约束)
     */

    // firstItem  View1(readOnly)
    id firstItem=layoutC.firstItem;
    // secondItem  View2 (readOnly)
    id secondItem=layoutC.secondItem;
    // firstAttribute View1的att (readOnly)
    NSLayoutAttribute firstAttribute=layoutC.firstAttribute;
    // secondAttribute View2的att(readOnly)
    NSLayoutAttribute secondAttribute=layoutC.secondAttribute;
    // relation 关系(readOnly)
    NSLayoutRelation relation=layoutC.relation;
    // multiplier 倍数(readOnly)
    CGFloat multiplier=layoutC.multiplier;

    // 约束的identifier
    @property (nullable, copy) NSString *identifier;
    // 
    @property BOOL shouldBeArchived;

iOS10
    // firstAnchor(readOnly)
    NSLayoutAnchor *firstAnchor=layoutC.firstAnchor;
    // secondAnchor(readOnly)
    NSLayoutAnchor *secondAnchor=layoutC.secondAnchor;
    // 获取 是否激活(默认false),只有active约束才生效
    BOOL isAc=layoutC.isActive;
    // 设置 是否激活
    [layoutC setActive:true];
    // 激活约束
    [NSLayoutConstraint activateConstraints:@[layoutC]];
    // 反激活约束
    [NSLayoutConstraint deactivateConstraints:@[layoutC]];

1.3.2. xib/Storyboard

    (1.  点击约束线,可配置此约束( multiplier 比例     Constant 值   Priority 优先级),delete可删除线)
    (2.  展开左侧查看视图大纲,和父视图(最小公视图)相关的约束放在父视图下,和自己大小相关的约束放在本视图下)
    (3.  左侧上方小箭头(frame和约束位置不一致时,更新frame(设置约束后没动-常用)或 更新约束(按现在的位置更新约束))
    (4.  小心ScrollView:约束SV的子视图时不要连SV(会连上它的ContentView),可以在SV的相同位置上创建一个View(SV在后上,覆盖))


方式一
            选中单个控件            点下方  画着一个矩形的图标        配置约束(见图知义, 选择据最近控件上下左右时【要选中相应的横线,现在填完数字后自动选中】、constrain to margins(一般不勾): 勾选之后,距离屏幕边界会多出20)
            选中多个控件(cmd)      点下方  画着两个矩形的图标        配置约束(见图知义)

方式二(不常用)    
            选中单个控件,ctr+拖到外右、外左、外上、外下、外斜、里左、里右、里斜     可进行配置。
单个控件约束 多个控件约束

1.3.3 NSLayoutAnchor(iOS 9)

NSLayoutAnchor<AnchorType> : NSObject
例

    [self.view setAutoresizesSubviews:false];
    
    //
    UIView *conView=[UIView new];
    [conView setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:conView];
    [conView setTranslatesAutoresizingMaskIntoConstraints:false];
    UIView *conView2=[UIView new];
    [conView2 setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:conView2];
    [conView2 setTranslatesAutoresizingMaskIntoConstraints:false];
    
    
    // iOS9(readOnly)
    NSLayoutConstraint *conViewLeftC=[conView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor constant:100];
    // iOS9(readOnly)
    NSLayoutConstraint *conViewTopC=[conView.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:100];
    // iOS9(readOnly)
    NSLayoutConstraint *conViewWidthC=[conView.widthAnchor constraintEqualToConstant:100];
    // iOS9(readOnly)
    NSLayoutConstraint *conViewHeightC=[conView.heightAnchor constraintEqualToConstant:100];
    
    // iOS9(readOnly)
    NSLayoutConstraint *conView2LeftC=[conView2.leftAnchor constraintEqualToAnchor:conView.leftAnchor constant:0];
    // iOS9(readOnly)
    NSLayoutConstraint *conView2TopC=[conView2.topAnchor constraintEqualToAnchor:conView.bottomAnchor constant:100];
    // iOS9(readOnly)
    NSLayoutConstraint *conView2WidthC=[conView2.widthAnchor constraintEqualToConstant:100];
    // iOS9(readOnly)
    NSLayoutConstraint *conView2HeightC=[conView2.heightAnchor constraintEqualToConstant:100];
    
    // 激活
    [NSLayoutConstraint activateConstraints:@[conViewLeftC,conViewTopC,conViewWidthC,conViewHeightC,conView2LeftC,conView2TopC,conView2WidthC,conView2HeightC]];
    /*
     readOnly(iOS9)
     NSLayoutXAxisAnchor *leadingAnchor
     NSLayoutXAxisAnchor *trailingAnchor
     NSLayoutXAxisAnchor *leftAnchor        leftAnchor不能和leadingAnchor比较
     NSLayoutXAxisAnchor *rightAnchor
     
     NSLayoutYAxisAnchor *topAnchor
     NSLayoutYAxisAnchor *bottomAnchor
     
     NSLayoutDimension *widthAnchor
     NSLayoutDimension *heightAnchor
     
     NSLayoutXAxisAnchor *centerXAnchor
     NSLayoutYAxisAnchor *centerYAnchor
     
     NSLayoutYAxisAnchor *firstBaselineAnchor
     NSLayoutYAxisAnchor *lastBaselineAnchor
     */
    // NSLayoutAnchor
    NSLayoutConstraint *leftC=[conView.leftAnchor constraintEqualToAnchor:conView2.leftAnchor constant:100];
    NSLayoutConstraint *leftC2=[conView.leftAnchor constraintEqualToAnchor:conView2.leftAnchor];
    NSLayoutConstraint *leftC6=[conView.leftAnchor constraintLessThanOrEqualToAnchor:conView2.leftAnchor];
    NSLayoutConstraint *leftC7=[conView.leftAnchor constraintGreaterThanOrEqualToAnchor:conView2.leftAnchor];
    NSLayoutConstraint *leftC8=[conView.leftAnchor constraintLessThanOrEqualToAnchor:conView2.leftAnchor constant:100];
    NSLayoutConstraint *leftC9=[conView.leftAnchor constraintGreaterThanOrEqualToAnchor:conView2.leftAnchor constant:100];


    // 仅NSLayoutXAxisAnchor:NSLayoutAnchor
    NSLayoutConstraint *leftC3=[conView.leftAnchor constraintEqualToSystemSpacingAfterAnchor:conView2.leftAnchor multiplier:1.0];
    NSLayoutConstraint *leftC4=[conView.leftAnchor constraintLessThanOrEqualToSystemSpacingAfterAnchor:conView2.leftAnchor multiplier:1.0];
    NSLayoutConstraint *leftC5=[conView.leftAnchor constraintGreaterThanOrEqualToSystemSpacingAfterAnchor:conView2.leftAnchor multiplier:1.0];


    // 仅NSLayoutYAxisAnchor:NSLayoutAnchor
    NSLayoutConstraint *topC=[conView.topAnchor constraintEqualToSystemSpacingBelowAnchor:conView2.topAnchor multiplier:1.0];
    NSLayoutConstraint *topC2=[conView.topAnchor constraintLessThanOrEqualToSystemSpacingBelowAnchor:conView2.topAnchor multiplier:1.0];
    NSLayoutConstraint *topC3=[conView.topAnchor constraintGreaterThanOrEqualToSystemSpacingBelowAnchor:conView2.topAnchor multiplier:1.0];


    // 仅NSLayoutDimension : NSLayoutAnchor
    NSLayoutConstraint *widthC=[conView.widthAnchor constraintEqualToConstant:100];
    NSLayoutConstraint *widthC2=[conView.widthAnchor constraintGreaterThanOrEqualToAnchor:conView2.widthAnchor constant:100];
    NSLayoutConstraint *widthC3=[conView.widthAnchor constraintLessThanOrEqualToAnchor:conView2.widthAnchor constant:100];
    NSLayoutConstraint *widthC4=[conView.widthAnchor constraintEqualToAnchor:conView2.widthAnchor multiplier:1.0];
    NSLayoutConstraint *widthC5=[conView.widthAnchor constraintLessThanOrEqualToAnchor:conView2.widthAnchor multiplier:1.0];
    NSLayoutConstraint *widthC6=[conView.widthAnchor constraintGreaterThanOrEqualToAnchor:conView2.widthAnchor multiplier:1.0];
    NSLayoutConstraint *widthC7=[conView.widthAnchor constraintEqualToAnchor:conView2.widthAnchor multiplier:1.0 constant:100];
    NSLayoutConstraint *widthC8=[conView.widthAnchor constraintLessThanOrEqualToAnchor:conView2.widthAnchor multiplier:1.0 constant:100];
    NSLayoutConstraint *widthC9=[conView.widthAnchor constraintGreaterThanOrEqualToAnchor:conView2.widthAnchor multiplier:1.0 constant:100];
1.4. SizeClass 配合Autolayout使用
    SizeClass实际上是对屏幕尺寸的抽象,把屏幕宽高分成Compact:紧凑、Regular:宽松、Any:任意三种类型这样就可以组合出九种不同的屏幕类型。

    在storyboard,xib编辑界面下最下方可以选择某一约束在只在某一类屏幕下生效.这样可以在不同屏幕下得到不同的UI布局效果.
2.三方库

2.1. Masonry

left      NSLayoutAttributeLeft 左侧
top       NSLayoutAttributeTop      上侧
right     NSLayoutAttributeRight    右侧
bottom    NSLayoutAttributeBottom   下侧
leading   NSLayoutAttributeLeading  首部
trailing  NSLayoutAttributeTrailing 尾部
width     NSLayoutAttributeWidth    宽
height    NSLayoutAttributeHeight   高
centerX   NSLayoutAttributeCenterX  横向中点
centerY   NSLayoutAttributeCenterY  纵向中点
baseline  NSLayoutAttributeBaseline 文本基线

方法列表

新增约束
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;
更新约束
- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;
清除之前所有约束,保留最新约束
- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;

注意:
1.新增约束(不能同时存在对同一对象的2个相同约束,必须先删除再新建)
2.立即更新约束[lunchView layoutIfNeeded];   动画要加(否则动画无效)

方法内容(不能写view.frame bounds 无效)

例:make.centerY.mas_equalTo(sv.mas_centerY);

offset:    距右距下(view的边界)
inset:     距左距上(view的边界)

equalTo();
greaterThanOrEqualTo();
lessThanOrEqualTo();
offset();

mas_equalTo();
mas_greaterThanOrEqualTo();
mas_lessThanOrEqualTo();
mas_offset();
mas_equalTo  比  equalTo  多了一个装箱操作     
#define mas_equalTo(...)                 equalTo(MASBoxValue((__VA_ARGS__)))
#define mas_greaterThanOrEqualTo(...)    greaterThanOrEqualTo(MASBoxValue((__VA_ARGS__)))
#define mas_lessThanOrEqualTo(...)       lessThanOrEqualTo(MASBoxValue((__VA_ARGS__)))
#define mas_offset(...)                  valueOffset(MASBoxValue((__VA_ARGS__)))
训练一:距父控件上下左右内边距10
make.edges.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
    /* 等价于
    make.top.equalTo(sv).with.offset(10);
    make.left.equalTo(sv).with.offset(10);
    make.bottom.equalTo(sv).with.offset(-10);   需要小于底
    make.right.equalTo(sv).with.offset(-10);    需要小于右
    */
// make.top.left.bottom.and.right等价于edges


训练二:两个控件垂直居中,相互间隔10
    // 
    int padding1 = 10;
    [sv2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(sv.mas_centerY);
        make.left.equalTo(sv.mas_left).with.offset(padding1);
        make.right.equalTo(sv3.mas_left).with.offset(-padding1);
        make.height.mas_equalTo(@150);
        make.width.equalTo(sv3);
    }];
    [sv3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(sv.mas_centerY);
        make.left.equalTo(sv2.mas_right).with.offset(padding1);
        make.right.equalTo(sv.mas_right).with.offset(-padding1);
        make.height.mas_equalTo(@150);
        make.width.equalTo(sv2);
    }];       

使用:

// 从此以后基本可以抛弃frame:CGRectMake了
UIView *sv = [UIView new];
// 在做autoLayout之前 一定要先将view添加到superview上 否则会报错
[self.view addSubview:sv];
// mas_makeConstraints就是Masonry的autolayout添加函数 将所需的约束添加到block中行了
[sv mas_makeConstraints:^(MASConstraintMaker *make) {

    // 将sv居中(横向+纵向)
    make.center.equalTo(self.view);

    // 设置size(300,300)
    make.size.mas_equalTo(CGSizeMake(300, 300));

    //
    make.width.height.mas_equalTo(70/2);
}];

2.2 UIView+AutoLayout.h/m

UIView+AutoLayout.hm文件地址

1. 导入 #import "UIView+AutoLayout.h"
2. 使用
    [bottomView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
    [bottomView autoAlignAxisToSuperviewAxis:ALAxisVertical];
    [bottomView autoAlignAxisToSuperviewAxis:ALAxisHorizontal];

    [bottomView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:0];
    [bottomView autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:0];
    [bottomView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
    [bottomView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];

    [bottomView autoSetDimension:ALDimensionWidth toSize:0];
    [bottomView autoSetDimension:ALDimensionHeight toSize:BOTTOM_HEIGHT];

    [[view1 autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:view2];
3.适配
ppi(pixel per inch):  每英寸有多少个像素。
    超过300ppi时,人眼便不能区分出每个像素。
          机型            坐标           像素        比例    物理尺寸         ppi
        iPhone4前       320*480       320*480      1:1    3.5 英寸    1X   
        iPhone4/4s      320*480       640*960      1:2    3.5 英寸    2X   326
        iPhone5/5s/5c   320*568       640*1136     1:2    4   英寸    2X   326
        iPhone6/7/8     375*667       750*1334     1:2    4.7 英寸    2X   326
        iPhonePlus      414*736       1080*1920    1:2.6  5.5 英寸    3X   401
        iPhoneX         375*812       1125*2436    1:3    5.8 英寸    3X   458

图片适配

文字,颜色等是矢量数据,放大并不会失真。
但图片不是矢量数据,必须作特殊处理,因此有@1x,@2x,@3x.

@2x :
    1坐标(即1个虚拟点,尺寸固定)对应4像素(即2*2 4像素)       

例:
    某图片(x,y,width,height) = (20,30,40,50) 表示:高度40个点,宽度50个点。

        iPhone4以前(单倍图) 则需要 40X1 * 50X1=40*50像素的图片
        iPhone4及后(2倍图)  则需要 40X2 * 50X2=80*100像素的图片,如果还是使用单倍图会失真。
        只需写图片名,@2x @3x 运行时自动选择(动态加载特性)

相关文章

网友评论

本文标题:布局 frame autoresizing autoLayout

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