-->

UIView的autoresizingMask - Interface Builder中的代码

2019-05-13 21:05发布

我已经制定了一些与子视图界面生成器,但我想这样做的代码,而不是。

我读过的UIView文档有关设置view.autoresizingMask财产。 我在寻找如何利用所提供的各种面具的支柱和弹簧翻译一个合乎逻辑的解释(如UIViewAutoresizingFlexibleLeftMargin等)。

Answer 1:

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.

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

    • vertical spring: UIViewAutoresizingFlexibleHeight or .flexibleHeight

    • horizontal spring: UIViewAutoresizingFlexibleWidth or .flexibleWidth

  • 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 or .flexibleLeftMargin

    • UIViewAutoresizingFlexibleRightMargin or .flexibleRightMargin

    • UIViewAutoresizingFlexibleTopMargin or .flexibleTopMargin

    • UIViewAutoresizingFlexibleBottomMargin or .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);



Answer 2:

UIViewAutoResizingMask s为我们所说的“支柱”和“弹簧”。 想想看:你有里面的小方形的大广场。 为了该方留完全居中,则必须从大正方形的各内侧边缘设置一个固定的宽度,以便限制它。 这些支柱。

春天,在另一方面,工作更像是一个UIView旋转过程中一样。 比方说,我们认为必须留在屏幕的底部,在中心(就像一个对准UIToolbar )。 我们要保持它的顶部弹簧弹性,这样,当视图旋转460像素到320像素,它保持它相对于屏幕同一位置现在是改变的尺寸。 在IB突出那些弹簧中的一个等于设置适当UIViewAutoResizingMask和突出顶部弹簧特别是类似于调用myView.autoResizingMask = UIViewAutoresizingFlexibleTopMargin

值可以通过串联在一对括号的将它们包围起来,并使用或操作者等中使用myView.autoResizingMask = (UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin)

因为他们是一个的typdef的口罩报告号码给你NSUInteger ,而这些都是苹果已经分配给他们的标志。 CMD +点击一个看到它的方法定义。



文章来源: UIView autoresizingMask - Interface Builder to Code - Programmatically create struts and springs - Swift or Objective-C