UIView autoresizingMask - Interface Builder to Code - 以编程方式创建struts和spring - Swift或Objective-C [英] UIView autoresizingMask - Interface Builder to Code - Programmatically create struts and springs - Swift or Objective-C

查看:416
本文介绍了UIView autoresizingMask - Interface Builder to Code - 以编程方式创建struts和spring - Swift或Objective-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Interface Builder制作了一些子视图,但我想在代码中进行。

I've laid out some subviews with Interface Builder, but I'd like to do it in code instead.

我读过UIView docs view.autoresizingMask属性。我正在寻找如何通过使用提供的各种掩码来翻译struts和spring的逻辑解释(例如 UIViewAutoresizingFlexibleLeftMargin 等)。

I've read the UIView docs about setting the view.autoresizingMask property. I'm looking for a logical explanation of how to translate the struts and springs by using the various masks offered (e.g. UIViewAutoresizingFlexibleLeftMargin, etc).

推荐答案

为视图设置自动调整大小时,使用按位包含OR( | )(目标 - C),或数组(Swift 2,3)指定弹簧 struts

When setting the autoresizing mask for a view, use a bitwise inclusive OR (|) (Objective-C), or an array (Swift 2, 3) to specify springs and struts.


  • 弹簧通过指定遮罩(分别为Objective-C或Swift 3)来表示:

  • Springs are represented by specifying a mask (Objective-C or Swift 3, respectively):


  • 垂直弹簧: UIViewAutoresizingFlexibleHeight .flexibleHeight

水平弹簧: UIViewAutoresizingFlexibleWidth .flexibleWidth

Struts 表示缺少四个灵活边距面具中的一个(即如果支柱是不存在,那个边缘的面具n被指定):

Struts are represented by the lack of one of the four 'flexible margin' masks (i.e. if a strut does not exist, the mask for that margin is specified):


  • UIViewAutoresizingFlexibleLeftMargin .flexibleLeftMargin

UIViewAutoresizingFlexibleRightMargin 。 flexibleRightMargin

UIViewAutoresizingFlexibleTopMargin .flexibleTopMargin

UIViewAutoresizingFlexibleBottomMargin .flexibleBottomMargin

例如,带有水平弹簧顶部和底部支柱将具有宽度,左右边距指定为灵活:

For example, a view with a horizontal spring and top and bottom struts would have the width, and left and right margins specified as flexible:

Swift 3

mySubview.autoresizingMask = [.flexibleWidth, .flexibleLeftMargin, .flexibleRightMargin]

Swift 2

mySubview.autoresizingMask = [.FlexibleWidth, .FlexibleLeftMargin, .FlexibleRightMargin]

Swift 1.2

mySubview.autoresizingMask = .FlexibleWidth | .FlexibleLeftMargin | .FlexibleRightMargin

Objective-C

mySubview.autoresizingMask = (UIViewAutoresizingFlexibleWidth |    
                              UIViewAutoresizingFlexibleLeftMargin |  
                              UIViewAutoresizingFlexibleRightMargin);

这篇关于UIView autoresizingMask - Interface Builder to Code - 以编程方式创建struts和spring - Swift或Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆