Recipe: Working with View Frames

When you change a view’s frame manually (rather than letting Auto Layout do the dirty work), you update its size (that is, its width and height) and its location. For example, you might move a frame as follows:

CGRect initialRect = CGRectMake(0.0f, 0.0f, 100.0f, 100.0f); myView = [[UIView alloc] initWithFrame:initialRect]; [topView addSubview:myView]; myView.frame = CGRectMake(0.0f, 30.0f, 100.0f, 100.0f);

This code creates a subview located at (0.0, 0.0) and then moves it down to (0.0, 30.0).

This approach for moving views is fairly uncommon. The iOS SDK does not expect you to move views by changing frames. Instead, it focuses on a view’s position. The preferred way to do this is ...

Get The Core iOS Developer’s Cookbook, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.