加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift开发:AutoResizing实现自动布局

发布时间:2020-12-14 07:02:27 所属栏目:百科 来源:网络整理
导读:/* autoresizing需求: 添加一个红色view ,放在主控制器的右下角,且距离右下角边框为20 不管横屏还是竖屏还是屏幕大小改变,红色view的位置不能改变,使用 代码添加autoresizing */ let redView = UIView() let wh:CGFloat = 100 let x:CGFloat = self.vie
         /*
        autoresizing需求:
        添加一个红色view ,放在主控制器的右下角,且距离右下角边框为20
        不管横屏还是竖屏还是屏幕大小改变,红色view的位置不能改变,使用
        代码添加autoresizing
        */
        let redView = UIView()
        
        let wh:CGFloat = 100
        let x:CGFloat = self.view.frame.size.width - wh - 20.0
        let y:CGFloat = self.view.frame.size.height - wh - 20.0
        redView.frame = CGRectMake(x,y,wh,wh)
        redView.backgroundColor = UIColor.redColor()
        
        
        var arm1 = UIViewAutoresizing.None
        
        arm1.unionInPlace(UIViewAutoresizing.FlexibleRightMargin)
        arm1.unionInPlace(UIViewAutoresizing.FlexibleLeftMargin)
        arm1.unionInPlace(UIViewAutoresizing.FlexibleBottomMargin)
        arm1.unionInPlace(UIViewAutoresizing.FlexibleTopMargin)
        
        redView.autoresizingMask = arm1
        
        print(redView.autoresizingMask)
        
        self.view.addSubview(redView)


oc写法

    UIView * redView = [[ UIView alloc]init];
    redView.backgroundColor = [UIColor redColor];
    CGFloat wh = 100;
    CGFloat x = self.view.frame.size.width - wh;
    CGFloat y = self.view.frame.size.height - wh;
    
    redView.frame = CGRectMake(x,wh);
    
    redView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
    
    [self.view addSubview:redView];

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读