/* 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];原文链接:https://www.f2er.com/swift/323932.html