xcode UIView autoresizingMask 用于固定左右边距和灵活宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16049009/
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
UIView autoresizingMask for fixed left and right margin and flexible width
提问by Alex Markman
I can't figure it out how to resize width with fixed left and right margin.
我无法弄清楚如何使用固定的左右边距调整宽度。
I don't find any fixed led/right margin APIs.
我没有找到任何固定的 LED/右边距 API。
回答by rmaddy
In code, to get a view to have fixed left and right margins along with flexible width you can do the following:
在代码中,要获得具有固定左右边距和灵活宽度的视图,您可以执行以下操作:
UIView *parentView = self.view; // adjust as needed
CGRect bounds = parentView.bounds; // get bounds of parent view
CGRect subviewFrame = CGRectInset(bounds, 20, 0); // left and right margin of 20
UIView *subview = [[UIView alloc] initWithFrame:subviewFrame];
subview.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[parentView addSubview:subview];
Adjust as needed to create your actual subview. Adjust the subviewFrame
to match your desired margins.
根据需要进行调整以创建您的实际子视图。调整subviewFrame
以匹配您所需的边距。
As answered, this will give your subview fixed left and right margins of 20 points each and a flexible width. When setting the autoresizingMask
, any component not set as flexible is automatically fixed (almost). This means the top margin and height are also fixed (since they are not set). The bottom margin is made implicitly flexible since the top margin and height are fixed. All three values going across or up/down can't be fixed at the same time for obvious reasons.
正如回答的那样,这将为您的子视图提供固定的左右边距,每个边距为 20 点,宽度灵活。设置 时autoresizingMask
,任何未设置为灵活的组件都会自动固定(几乎)。这意味着顶部边距和高度也是固定的(因为它们没有设置)。由于顶部边距和高度是固定的,因此底部边距是隐式灵活的。出于显而易见的原因,无法同时固定所有三个跨越或向上/向下的值。