You are currently browsing the tag archive for the ‘uiview’ tag.

Some classes (like UITableView) will automatically resize when the device orientation changes, e.g. from portrait to landscape, but others (like UILabel and UITextView) require a little configuration.

To begin, let’s start with a few assumptions. For a UILabel, we’ll assume it contains multiple lines of text and is configured (in Interface Builder or in code) to take up 100% of the screen width. For a UITextView, we’ll assume the same and in addition, that it takes up the remainder of the screen height.

Here’s an example of what this would look like in Interface Builder:

If you create these objects in IB (like in the example above) then you’ll also need to connect them to properties defined in your view controller’s header file.

Now we’re ready to configure the objects in your view controller implementation. The salient details will be in the viewDidLoad method:

– (void)viewDidLoad
{
    [super viewDidLoad];

    [self.myLabel setAutoresizesSubviews:YES];
    [self.myLabel setAutoresizingMask:
        UIViewAutoresizingFlexibleWidth];

    [self.myTextView setAutoresizesSubviews:YES];
    [self.myTextView setAutoresizingMask:
        UIViewAutoresizingFlexibleWidth |
        UIViewAutoresizingFlexibleHeight];
}


The setAutoresizesSubviews property controls whether each object will resize automatically when their bounds change.

The setAutoresizingMask property controls how each object resizes automatically. A UILabel only has to worry about resizing its width, but since a UITextView is scrollable it needs to resize both its width and height when its bounds change.

You should also make sure the shouldAutorotateToInterfaceOrientation method is configured to return YES; otherwise your view won’t do anything when the device orientation changes!


Aggregated by Adobe Feeds