Wednesday, August 15, 2012

iOS, iPhone, iPad, XCode resize view on rotation


This is quick blog on how to resize view in iPhone, iPad application. Normally when we use story board all the views we add are of portrait mode and if your app supports rotation, in landscape mode you need to resize your views. So how to do that.

Go to your ViewController file and find the following function.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

Normally function will look as following.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);

That means it will only support Portrait orientation. So to allow rotation in your app change this function as follow.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;

Now it will allow rotation in application. Now add one more function to your view controller file

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    if (fromInterfaceOrientation == UIInterfaceOrientationPortrait) {

This function will be invoked automatically when orientation of phone changes. fromInterfaceOrientation will give you the last orientation. That means if you are in porttrait mode and you are now changing to landscape mode, value of fromInterfaceOrientation would be UIInterfaceOrientationPortrait. So in above function you need to reize your view in if else loop.

Hope this helps you.

No comments:

Post a Comment