Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView autoresizingMask for fixed left and right margin and flexible width

I can't figure it out how to resize width with fixed left and right margin.Autoresize xCode

I don't find any fixed led/right margin APIs.

like image 871
Alex Markman Avatar asked Apr 16 '13 23:04

Alex Markman


1 Answers

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.

like image 62
rmaddy Avatar answered Nov 26 '22 08:11

rmaddy