美文网首页iOS专题
iOS基础(二) - autoresizing使用心得

iOS基础(二) - autoresizing使用心得

作者: 一剑孤城 | 来源:发表于2016-12-01 17:38 被阅读16次

    前言:
    autoresizing是iOS在autoLayout出现之前的界面自动化布局方式,但是,由于该方法的局限性,只能用于子控件适应父控件的布局改变,所以,苹果官方后面出了autoLayout,一种能适应更复杂场景的自动化布局方式。

    1.简单的使用

    \\子控件相对于父控件右边的间距不变
    UIView *testView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 120, 80)];
    testView.backgroundColor = [UIColor orangeColor];
    
    UIView *otherView = [[UIView alloc] initWithFrame: CGRectMake(5, 5, 40, 40)];
    otherView.backgroundColor = [UIColor blueColor];
    otherView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;  //左边间距可变,那么默认其它间距不变,比如顶部间距,底部间距和右边间距,宽高等等
    
    [testView addSubview: otherView];
    [self.view addSubview: testView];
    
    //改变父控件的frame
    testView.frame = CGRectMake(0, 0, 200, 120);
    

    效果图:


    autoMaskLeft.gif

    上面的动图除了左边间距改变了,底部间距也改变了,因为这里涉及到两个间距,一个就是底部间距,还有一个就是控件本身的高度,很明显高度不变的优先级比底部间距不变的优先级高,所以,底部间距改变了,而高度不变。

    2.view的autoresizingMask属性

    typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
        UIViewAutoresizingNone                 = 0,          //默认选项,效果就是顶部和左边距离父控件不变,并且宽高也不变
        UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,     //与父控件右边间距不可变,左边可变
        UIViewAutoresizingFlexibleWidth        = 1 << 1,     //视图宽度可变
        UIViewAutoresizingFlexibleRightMargin  = 1 << 2,     //与父控件左边间距不可变,右边可变
        UIViewAutoresizingFlexibleTopMargin    = 1 << 3,     //与父控件底部间距不可变,顶部可变
        UIViewAutoresizingFlexibleHeight       = 1 << 4,     //视图高度可变
        UIViewAutoresizingFlexibleBottomMargin = 1 << 5      //与父控件顶部间距不可变,底部可变
    };
    //注:由于是options,所以可以多选,用"|"符隔开就行
    

    3.罗列出基本选项的效果以及一些组合选项的效果
    (1)右边间距可变

    otherView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
    
    autoMaskRight.gif

    (2)顶部间距可变

    otherView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    
    autoMaskTop.gif

    (3)底部间距可变

    otherView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
    
    autoMaskBottom.gif

    (4)左边和右边的间距可变

    otherView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    
    autoMaskLToR.gif

    (5)宽度可变

    otherView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    
    autoMaskWidth.gif

    (6)高度可变

    otherView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    
    autoMaskHeight.gif

    (7)高度和宽度可变

    otherView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
    autoMaskWAndH.gif

    (8)左边间距和高度可变

    otherView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
    
    autoMaskLAndH.gif

    (9)左边间距和宽度可变

    otherView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth;
    
    autoMaskLAndW.gif

    注:还可以三个属性以及更多属性组合,可以自己尝试一下各种组合效果如何,这里不做过多说明。

    4.总结
    在最后点个题,使用autoresizing来自动化布局局限性还是很明显的,从苹果只给出了一个可选的属性就可以知道,变化空间不大。所以,如果想要更复杂的效果,还是选择autoLayout,无论原生的还是第三方库。

    相关文章

      网友评论

      • Hello_kid:autoresizingMask 就是用frame固定好,配合autoresizingMask这个使用,可以这么理解么?
        一剑孤城:可以这样理解
      • Hello_kid:您好,这个动态变化动图是怎么实现的?

      本文标题:iOS基础(二) - autoresizing使用心得

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