美文网首页
iOS开发之Autoresizing记录

iOS开发之Autoresizing记录

作者: 朱晓晓的技术博客 | 来源:发表于2018-06-26 14:59 被阅读66次

    Autoresizing,仅限于父子控件之间的操作,对于兄弟控制器的操作是无效的

    拖控件

    1.取消自动布局
    image.png
    1.设置布局
    image.png

    纯代码

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        //autoresizing
        
        UIView *bgView = [[UIView alloc]init];
        bgView.backgroundColor = [UIColor orangeColor];
        bgView.frame = CGRectMake(0, 0, 200, 250);
        [self.view addSubview:bgView];
        self.bgView = bgView;
        
        
        
        UIView *childView = [[UIView alloc]init];
        childView.frame = CGRectMake(150, 150, 50, 100);
        childView.backgroundColor = [UIColor redColor];
        [bgView addSubview:childView];
        childView.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleTopMargin |UIViewAutoresizingFlexibleLeftMargin;
        
    
    }
    
    
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        
        CGFloat w  = 100 +arc4random_uniform(100);
        CGFloat h =  100+ arc4random_uniform(100);
        self.bgView.frame = CGRectMake(0, 0, w, h);
    }
    
    

    重点提示 UIViewAutoresizing

         UIViewAutoresizingNone                 = 0, 什么都没有
         UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,距离右边固定
         UIViewAutoresizingFlexibleRightMargin  = 1 << 2,距离左边固定
         UIViewAutoresizingFlexibleTopMargin    = 1 << 3,距离底部固定
         UIViewAutoresizingFlexibleBottomMargin = 1 << 5 距离顶部固定
    
         以上的几个属性跟咱们拖控件时候是相反的
    
         
         UIViewAutoresizingFlexibleWidth        = 1 << 1,宽度随父控件自行伸缩
         UIViewAutoresizingFlexibleHeight       = 1 << 4,宽度随父控件自行伸缩
    
    

    相关文章

      网友评论

          本文标题:iOS开发之Autoresizing记录

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