user635064
user635064

Reputation: 6247

Position of UIScrollView after change in orientation

I have a UIScrollView with an image inside it. This is the code:

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    UIScrollView *imScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 96)];
    [imScrollView addSubview:imView];
    [imScrollView setContentSize:CGSizeMake(530, 96)];
    [[self view] addSubview:imScrollView];
    [imView release];
    [imScrollView release];
}

This is what it looks like in portrait mode: enter image description here

I want to know 1. How can I position the image exactly in the middle of the screen (horizontally). 2. How can I make sure this is going to be the same if the orientation switches to landscape. This is what currently the landscape looks like : enter image description here

Thanks.

Upvotes: 0

Views: 499

Answers (1)

occulus
occulus

Reputation: 17014

Try setting the autoresizingMask property of your imView just after you create it:

imView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin;

The UIView class reference contains more info.

Upvotes: 1

Related Questions