Resizing UIViews

21k views Asked by At

If I need to resize or reposition a UIView, is there a better way to do it than the following?

view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, view.frame.size.height - 100);

Or in other words, is there a way to just say view.frame.size.height -= 100 through a non-readonly property?

3

There are 3 answers

0
Aviad Ben Dov On BEST ANSWER

If you want the -=100 effect, just do:

CGRect frame = view.frame;
frame.size.height -= 100;
view.frame = frame;

Then you can be certain you're changing exactly what you want to change, too..

Other than that, I don't think there's a way..

0
David M. On

Use this UIView+position category to get convenient property access to the frame's members:

http://bynomial.com/blog/?p=24

2
dmercredi On

There isn't a property to directly manipulate the height.

Note that every time you call view.frame it calls the method, so your code is equivilent to:

view.frame = CGRectMake([view frame].origin.x, [view frame].origin.y, [view frame].size.width, [view frame].size.height - 100);

Consider instead adjusting the frame in a separate variable:

CGRect frame = view.frame;
frame.size.height -= 100;
view.frame = frame;