如何以编程方式设置设备(UI)方向?
希望屏幕上的所有内容(UI)能够从左到右旋转或反过来。
我怎么去做这个? 这是私人的吗?
我知道
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {}
可以让你说出UI的取向,但是有办法一次只强制一个?
干杯
该方法被调用,以确定您的界面是否应该自动旋转到给定的旋转(即让UIKit辛勤工作,而不是你手动)。
所以,如果你想让你的应用程序只在横向工作,你可以用下面的方法来实现这个方法的主体:
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
如果你想要你的用户界面自动旋转到所有的方向,你可以只return YES;
这是你问的吗?
在iOS中[[UIDevice currentDevice] setDeviceOrientation:UIDeviceOrientationSomeOrientation]
方法不存在。 但是我们可以用状态栏旋转视图,为此:
- (void)showAlbum { // check current orientation if ([[UIApplication sharedApplication] statusBarOrientation] != UIInterfaceOrientationLandscapeLeft) { // no, the orientation is wrong, we must rotate the UI self.navigationController.view.userInteractionEnabled = NO; [UIView beginAnimations:@"newAlbum" context:NULL]; [UIView setAnimationDelegate:self]; // when rotation is done, we can add new views, because UI orientation is OK [UIView setAnimationDidStopSelector:@selector(addAlbum)]; // setup status bar [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO]; // rotate main view, in this sample the view of navigation controller is the root view in main window [self.navigationController.view setTransform: CGAffineTransformMakeRotation(M_PI / 2)]; // set size of view [self.navigationController.view setFrame:CGRectMake(0, 0, 748, 1024)]; [UIView commitAnimations]; } else { [self addAlbum]; } }
我已经成功了:
if (self.interfaceOrientation != UIInterfaceOrientationPortrait) { // http://stackoverflow.com/questions/181780/is-there-a-documented-way-to-set-the-iphone-orientation // http://openradar.appspot.com/radar?id=697 // [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait]; // Using the following code to get around apple's static analysis... [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait]; }
如果您呈现的模式视图控制器实现了-shouldAutorotateToInterfaceOrientation:
只支持一个方向,整个界面将自动被强制进入。 我不认为有一种方法来编程改变方向,否则。
这个简单的方法也适用:
[[UIApplication sharedApplication] setStatusBarHidden:YES]; [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
然后回来:
[[UIApplication sharedApplication] setStatusBarHidden:NO]; [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
在Swift中将方向改为肖像:
UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")
请参阅: https : //stackoverflow.com/a/24259601
这是通过:Guntis Treulands https://stackoverflow.com/a/10146270/894671
//----------------------------------- // FORCE PORTRAIT CODE //----------------------------------- [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationPortrait animated:NO]; //present/dismiss viewcontroller in order to activate rotating. UIViewController *mVC = [[UIViewController alloc] init]; [self presentModalViewController:mVC animated:NO]; [self dismissModalViewControllerAnimated:NO];
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeLeft ); }
即使这不是一个闲置的解决scheme,适合我。
- (void)viewDidLoad { self.view.transform = CGAffineTransformIdentity; self.view.transform = CGAffineTransformMakeRotation((M_PI * (90) / 180.0)); self.view.bounds = CGRectMake(0.0, 0.0, 480, 320); }
正如相关线程build议 shouldAutorotateToInterfaceOrientation
已被弃用。 较新的应用程序应该使用苹果的UIViewController类参考中所述的以下方法,直到它们自己被弃用为止:
-
shouldAutorotate
, -
preferredInterfaceOrientationForPresentation
; 和 -
attemptRotationToDeviceOrientation
。
根据我的理解,最好使用不同的视图控制器来查看需要locking(即有一个首选)的方向模式,这与上一个不同。