UIView autoresizingMask用于固定左右边距和灵活宽度

Ale*_*man 8 xcode objective-c ios

我无法弄清楚如何使用固定的左右边距调整宽度.自动调整xCode

我找不到任何固定的领先/右边距API.

rma*_*ddy 22

在代码中,要使视图具有固定的左右边距以及灵活的宽度,您可以执行以下操作:

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];
Run Code Online (Sandbox Code Playgroud)

根据需要进行调整以创建实际的子视图.调整subviewFrame以匹配您想要的边距.

如上所述,这将为您的子视图提供固定的左右边距,每个边距为20个点,并具有灵活的宽度.设置时autoresizingMask,任何未设置为灵活的组件都会自动固定(几乎).这意味着上边距和高度也是固定的(因为它们未设置).由于上边距和高度是固定的,因此底边距是隐式灵活的.由于显而易见的原因,所有三个值都不能同时修复.