Reputation: 139
my App has several uiViews and set to support both orientations. since my uiViews frames are only part of the whole size of the iPad, so I'm trying to center my uiviews frame depending on how the iPad is held aka orientation. It's is done in this manner in the view controller.m file:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
UIInterfaceOrientation des=self.interfaceOrientation;
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) //iPad
{
CGRect ScreenBounds = [[UIScreen mainScreen] bounds];
if(des==UIInterfaceOrientationPortrait||des==UIInterfaceOrientationPortraitUpsideDown)//ipad-portrait
{
ipStPosX=(ScreenBounds.size.width -360)/2;
ipStPosY=100;
return YES;
}
else//ipad -landscape
{
ipStPosX=(ScreenBounds.size.height -360)/2;
ipStPosY=100;
return YES;
}
}
else//iphone
{
UIInterfaceOrientation des=self.interfaceOrientation;
if(des==UIInterfaceOrientationPortrait||des==UIInterfaceOrientationPortraitUpsideDown) //iphone portrait
{
return NO;
}
else //iphone -landscape
{
return YES;
}
}
}
after launching the app and changing the orientation it will always go to the portrait part, UIInterfaceOrientationPortrait, regardless of how i hold the device.
I saw some old posts that didn't really fit the issue and were confusing or dated so any help here will be great.
Upvotes: 0
Views: 485
Reputation: 835
I think there are two problems here:
Enabling rotation - This can be done by implementing the method below. You should put a breakpoint in this method to ensure its called and you return YES. Please note that this method is only called for those views that are pushed to the navigation bar or tab bar or are part of the window. This will not be called for views that have been added to other views using addSubView method.
Implementing the above method and not implementing any other method will rotate your view. If it does not, you need to investigate the points I have outlined above. Once your view starts rotating, you need to then play with the autoresizing masks to achieve what you are planning to do. Centering the views should be easily achievable using autoresizing masks. If using XIBs, you can review the resizing behavior from within xcode.
Upvotes: 0
Reputation: 7563
shouldAutorotateToInterfaceOrientation:
is for simply telling iOS that you support particular orientations. since you want everything but iPhone landscape, you should return
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) //iPad
return YES;
return UIInterfaceOrientationIsPortrait(orientation);
}
for actually adjusting what you display, you want to use willRotateToInterfaceOrientation:duration:
or didRotateFromInterfaceOrientation:
for actually changing items. in your case, given that you are just adjusting some iVars depending upon which orientation you are in, i presume you will use the latter.
- (void)didRotateToInterfaceOrientation:(UIInterfaceOrientation)fromOrientation
UIInterfaceOrientation des = self.interfaceOrientation;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) //iPad
{
CGRect ScreenBounds = [[UIScreen mainScreen] bounds];
if (UIInterfaceOrientationIsPortrait(des))//ipad-portrait
{
ipStPosX=(ScreenBounds.size.width -360)/2;
ipStPosY=100;
}
else //ipad -landscape
{
ipStPosX=(ScreenBounds.size.height -360)/2;
ipStPosY=100;
}
}
}
Upvotes: 1