Search code examples
iphoneiosxcode4

Best way to detect iphone orientation


I am making an app where I change the buttons layout depending upon if the user moved his iphone in portrait vs landscape mode. I thought the best place to put my code will be in

-(BOOL) shouldautorotatetointerfaceorientation

But there are two major problems with that

  1. When the page is loaded shouldautorotatetointerfaceorientation is called at least 3 times. I want to execute my code only once.

  2. Even when I move from viewController1 (page1) to viewController2 (page2) with Modal segue and I rotate my iphone on page2 it still calls the autorate code which is in my viewcontroller1. That is very strange.

Anyways, what is the best place to put my orientation based icons in?


Solution

  • The sole purpose of the method

    -(BOOL) shouldautorotatetointerfaceorientation
    

    is to determine which orientations are supported.

    If you wish to do custom actions on rotation, then you want to implement one or both of the method

    -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration