Tuesday, April 19, 2011

shouldAutorotateToInterfaceOrientation example objc objective c

By default, shouldAutorotateToInterfaceOrientation returns YES for the UIInterfaceOrientationPortrait orientation only. If your view controller supports additional orientations, override this method and return YES for all orientations it supports. Your implementation of shouldAutorotateToInterfaceOrientation should simply return YES or NO based on the value in the interfaceOrientation parameter. Do not attempt to get the value of the interfaceOrientation property or check the orientation value reported by the UIDevice class. Your view controller is either capable of supporting a given orientation or it is not

iPhone supports four rotation modes.

UIInterfaceOrientationPortrait
UIInterfaceOrientationPortraitUpsideDown
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight



If you want only "Portrait mode". You can use following code.


- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


If you want to support all the orientation.


- (BOOL)shouldAutorotateToInterfaceOrientation:

(UIInterfaceOrientation)interfaceOrientation {

return YES;

}



If you want to support some of the orientations, you can write

 (here Portrait, landscapeLeft, landscapeRight)


- (BOOL)shouldAutorotateToInterfaceOrientation:

(UIInterfaceOrientation)interfaceOrientation {

return (interfaceOrientation == UIInterfaceOrientationPortrait ||

interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||

interfaceOrientation == UIInterfaceOrientationLandscapeRight);

}

Just returning YES in this method isn't enough, you should also set auto-size parameters of the view in the interface builder.