I have a universal application created in storyboard mode. It is set-up to automatically rotate to the current orientation. I have attempted to disable some orientations in iPhone-mode but it does not work when I test in the simulator.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return ((interfaceOrientation != UIInterfaceOrientationLandscapeRight) || (interfaceOrientation != UIInterfaceOrientationLandscapeLeft));
} else {
return YES;
}
}
Also, when the iPad is rotated, it should go through the following code BUT all I get is a black screen.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
self.view = landscape;
self.view.transform = CGAffineTransformMakeRotation(deg2rad*(90));
self.view.bounds = CGRectMake(0.0, 0.0, 1024.0, 748.0);
} else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
self.view = landscape;
self.view.transform = CGAffineTransformMakeRotation(deg2rad*(-90));
self.view.bounds = CGRectMake(0.0, 0.0, 1024.0, 748.0);
} else {
self.view = portrait;
self.view.transform = CGAffineTransformMakeRotation(0);
self.view.bounds = CGRectMake(0.0, 0.0, 768.0, 1004.0);
}
} else {
}
}
Why are all these problems arising here? I have tried the code in different Xcode projects without storyboards ad it works fine. What is going on and how can I fix this?
You should create two separate stackoverflow questions. I will answer your first question here.
Consider this line from your code:
If the orientation is
Right, then your code reduces toreturn (Right != Right) || (Right != Left), which always returns true.If the orientation is
Left, then your code reduces toreturn (Left != Right) || (Left != Left), which always returns true.If the orientation is
Up, then your code reduces toreturn (Up != Right) || (Up != Left), which always returns true.If the orientation is
Down, then your code reduces toreturn (Down != Right) || (Down != Left), which always returns true.It’s not clear which orientations you want to allow and which you want to exclude. If you want to allow only portrait orientations, do this:
If you want to allow only landscape orientations, do this: