Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

iphone - Device orientation - change in legal way (iOS 4.0+)

My app has navigation bar, where 1st screen returns YES to orientation, the second one is set to some orientation basing on what user choose in 1st screen. After going back to 1st screen from 2nd one, if user had device in hand in portrait but interface was in landscape, 1st screen is set to landscape. This happens because of

(BOOL)shouldAutorotateToInterfaceOrientation(UIInterfaceOrientation)interfaceOrientation 

Is called only after changing device orientation.

I want to check what is device orientation atm and set interface orientation to this one. Tried:

//1st method:
UIViewController *rotateTheScreen = [[UIViewController alloc]init];
[self presentModalViewController:rotateTheScreen animated:NO];
[self dismissModalViewControllerAnimated:NO];
[rotateTheScreen release];

//2nd method:
UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;

//3rd method:
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

1st is acting strange, rotates all cases besides coming back from interface orientation = landscape and device orientation = landscape (here is a bug, he rotates to landscape) 2nd checks interface, like the name tells, and tho doesnt work for my problem 3rd as far as i heard is private and Apple rejects apps using this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Take a look at this thread. Basically, there's no way to force a device orientation and get your application approved by Apple.

Shortly, the method exists but is an undocumented method of the UIDevice class.

[[UIDevice currentDevice] setOrientation: UIInterfaceOrientationLandscapeRight animated:YES];

This gives a compiler warning that you can get rid of with a category.

@interface UIDevice (MyAwesomeMethodsThatAppleWillNeverAllowMeToUse)
    -(void)setOrientation:(UIInterfaceOrientation)orientation animated:(BOOL)animated;
    -(void)setOrientation:(UIInterfaceOrientation)orientation;
@end

Also, some say that you can call these methods indirectly using performSelector to get around Apple's static code analysis, as you can read in the comments here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...