Domanda

I have been successful in viewing app in portrait mode. What the problem is, when I view it in landscape, it doesn't seem to be proper. I have implemented this code.

if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft)
{
    return interfaceOrientation=UIInterfaceOrientationLandscapeLeft;
}
else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
{
    return interfaceOrientation=UIInterfaceOrientationLandscapeRight;
}
else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait)
{
   return interfaceOrientation=UIInterfaceOrientationPortrait;
}   

Should I do anything extra? Is this code helpful to me?

È stato utile?

Soluzione

Change your implementation of shouldAutorotateToInterfaceOrientation: into the following if you want to support all possible orientations:

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

If you want to support only: UIInterfaceOrientationLandscapeLeft, UIInterfaceOrientationLandscapeRight and UIInterfaceOrientationPortrait, then change your code into:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}

You don't need to look for the new orientation in [UIApplication sharedApplication].statusBarOrientation because you already have it as a parameter.


If you want to make the elements of the view repositioned when the device is rotated, you have to use their autoresizingMask property. Another option is to implement the method willRotateToInterfaceOrientation.

You can change autoresizingMask of the elements of the view in Interface Builder in the Size Inspector. Try experimenting with different combination until you get the result you need.

enter image description here

Finally, note that if your view is complicated or is radically different in each orientation, the best option is to use two xib files: one for portrait and one for landscape.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top