Ken
Ken

Reputation: 636

What are some best practices I should be using when dealing with multiple orientations?

Please forgive my ignorance. Traditionally I've been doing something like this, but it feels like a lot of unnecessary work and I'm not sure if there are easier ways to accomplish this.

// logoView dimensions
static const CGFloat LOGO_VIEW_X_PORT = 42;
static const CGFloat LOGO_VIEW_Y_PORT = 20;
static const CGFloat LOGO_VIEW_W_PORT = 237;
static const CGFloat LOGO_VIEW_H_PORT = 82;
// 
static const CGFloat LOGO_VIEW_X_LAND = 142;
static const CGFloat LOGO_VIEW_Y_LAND = 20;
static const CGFloat LOGO_VIEW_W_LAND = 237;
static const CGFloat LOGO_VIEW_H_LAND = 82;

- (void)viewDidLoad {
    [super viewDidLoad];

    // init logo view
    self.logoView = [[UIImageView alloc] initWithImage:...];

    // determine device orientation and set dimensions 
    if (self.interfaceOrientation == UIDeviceOrientationPortrait || 
        toInterfaceOrientation == UIDeviceOrientationPortraitUpsideDown) {
        self.logoView.frame = CGRectMake(LOGO_VIEW_X_PORT, LOGO_VIEW_Y_PORT, LOGO_VIEW_W_PORT, LOGO_VIEW_H_PORT);
    } else {
        self.logoView.frame = CGRectMake(LOGO_VIEW_X_LAND, LOGO_VIEW_Y_LAND, LOGO_VIEW_W_LAND, LOGO_VIEW_H_LAND);
    }

    [self.view addSubview:self.logoView];

}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if (toInterfaceOrientation == UIDeviceOrientationPortrait || 
        toInterfaceOrientation == UIDeviceOrientationPortraitUpsideDown) {
        self.logoView.frame = CGRectMake(LOGO_VIEW_X_PORT, LOGO_VIEW_Y_PORT, LOGO_VIEW_W_PORT, LOGO_VIEW_H_PORT);
        // long list of other views...
    } else {
        self.logoView.frame = CGRectMake(LOGO_VIEW_X_LAND, LOGO_VIEW_Y_LAND, LOGO_VIEW_W_LAND, LOGO_VIEW_H_LAND);
        // long list of other views...
    }
}

Is there a better protocol I should be following to accomplish this, or is this more or less how everyone handles multiple orientations?

Upvotes: 0

Views: 59

Answers (1)

Niko
Niko

Reputation: 2543

Have a look on the autoResizingMask option of all graphical objects. That should help.

Upvotes: 1

Related Questions