Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting current interface orientation in iOS 6

I do the orientation checking dynamically in iOS 5.x as below:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
 if(interfaceOrientation == UIInterfaceOrientationPortrait||interfaceOrientation ==         UIInterfaceOrientationPortraitUpsideDown) 
 {
self.viewProfile.frame = CGRectMake(238, 612, 295, 73);
// some more settings
}
else
{
self.profileNew.frame = CGRectMake(374, 540, 295, 58);
 // some more settings
}
 return YES;
}

For iOS 6, I did the below code but thats not working:

-(BOOL)shouldAutorotate{    
   return YES;
}

-(NSInteger)supportedInterfaceOrientations
{
 if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)) 
  {
   self.viewProfile.frame = CGRectMake(238, 612, 295, 73);
   // some more settings
  }
  else
  {
   self.profileNew.frame = CGRectMake(374, 540, 295, 58);
   // some more settings
  }
 return UIInterfaceOrientationMaskAll;
}

How can I check the interface oriention in iOS 6 just like I did in iOS 5.x?

Thanks

like image 241
Noufal Kmc Avatar asked Dec 20 '12 08:12

Noufal Kmc


3 Answers

You can use the viewWillLayoutSubviews method, where you check the orientation whith

[[UIApplication sharedApplication] statusBarOrientation];

and set your frames accordingly.

Hope this helps!

like image 55
Levi Avatar answered Oct 21 '22 16:10

Levi


Try to do the check in this method

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

Edit:

Use this:

BOOL isInPortrait = UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]);
like image 30
Nikola Kirev Avatar answered Oct 21 '22 17:10

Nikola Kirev


Combined Levi's and Nikola Kirev's answers and now the code is working properly. Thanks both of you. Here is the code for others to reference :

For iOS 6 :

 -(BOOL)shouldAutorotate{
    return YES;
  }


-(NSInteger)supportedInterfaceOrientations{
        if (UIDeviceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) {
            self.viewProfile.frame = CGRectMake(238, 612, 295, 73);
            //other codes            
        }

        else {
            self.viewProfile.frame = CGRectMake(374, 462, 295, 58);
            //other codes
        }
        return UIInterfaceOrientationMaskAll;
       }

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
                            duration:(NSTimeInterval)duration{
    if (UIDeviceOrientationIsPortrait(orientation)) {
        self.viewProfile.frame = CGRectMake(238, 612, 295, 73);
        //other codes
        self.profileNew.frame = CGRectMake(238, 713, 295, 73);            
    }

    else {
        self.viewProfile.frame = CGRectMake(374, 462, 295, 58);
        //other codes
    }
}

For iOS 5.x and 4.x

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if(interfaceOrientation == UIInterfaceOrientationPortrait||interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        self.viewProfile.frame = CGRectMake(238, 612, 295, 73);
        //other codes      
    }

    else {
        self.viewProfile.frame = CGRectMake(374, 462, 295, 58); 
        //other codes
        }
    return YES;
    } 
}
like image 1
Noufal Kmc Avatar answered Oct 21 '22 17:10

Noufal Kmc