ios 以编程方式设置 UIView 的自动调整大小掩码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12695343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set UIView's autoresizing mask programmatically?
提问by Hiren
I have to set autoresizingMask programmatically for UIView
.
我必须以编程方式为UIView
.
I don't know how to implement this.
我不知道如何实现这一点。
回答by pnizzle
To achieve what you have in that screen shot you need to do the opposite of what DrummerB suggests. You want a fixed top margin so you make every other side flexible like so:
要实现该屏幕截图中的内容,您需要执行与 DrummerB 建议相反的操作。你想要一个固定的上边距,这样你就可以让每一边都灵活,像这样:
Objective C:
目标 C:
view.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleBottomMargin;
Not setting a side as flexible means that it will be fixed (default behaviour), thats why there is no such thing as UIViewAutoResizingFixedTopMargin (since its the same as not setting UIViewAutoresizingFlexibleTopMargin)
不将一侧设置为灵活意味着它将被修复(默认行为),这就是为什么没有 UIViewAutoResizingFixedTopMargin 这样的东西(因为它与不设置 UIViewAutoresizingFlexibleTopMargin 相同)
Edit for Swift:
为 Swift 编辑:
view.autoresizingMask = [.FlexibleRightMargin, .FlexibleLeftMargin, .FlexibleBottomMargin]
Credit to Tom Calmon for adding the swift version 1st.
感谢 Tom Calmon 添加 swift 版本 1st。
Swift 5.0 update:
斯威夫特 5.0 更新:
view.autoresizingMask = [.flexibleRightMargin, .flexibleLeftMargin, .flexibleBottomMargin]
Cheers.
干杯。
回答by DrummerB
You have to set the view's autoresizingMask
property:
您必须设置视图的autoresizingMask
属性:
view.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
The possible values are defined in UIViewAutoresizing
:
可能的值定义在UIViewAutoresizing
:
enum {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
typedef NSUInteger UIViewAutoresizing;
You can set multiple values with the bitwise OR operator |
.
您可以使用按位 OR 运算符设置多个值|
。
回答by Thomás Calmon
Swift 2.0:
斯威夫特 2.0:
view.autoresizingMask = [.FlexibleRightMargin, .FlexibleLeftMargin, .FlexibleBottomMargin]
回答by Jimmy_m
Swift 4.1:
斯威夫特 4.1:
view.autoresizingMask = [.flexibleHeight, .flexibleWidth, .flexibleTopMargin, .flexibleRightMargin, .flexibleLeftMargin, .flexibleBottomMargin]
回答by Jimmy_m
To set flexible Top Margin,Bottom Margin,Left Margin and Right Margin of a UIView write the following code-
要设置 UIView 的灵活上边距、下边距、左边距和右边距,请编写以下代码-
autoresizingMask=UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;