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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With