希望屏幕上的所有内容(UI)都能够从左横屏旋转到右横屏或反之亦然。
我应该如何做?这是私密的吗?
我知道
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {}
UIInterfaceOrientationMask可以用来指定UI支持的屏幕方向,但有没有一种方法可以强制只允许一种方向?
谢谢。
希望屏幕上的所有内容(UI)都能够从左横屏旋转到右横屏或反之亦然。
我应该如何做?这是私密的吗?
我知道
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {}
UIInterfaceOrientationMask可以用来指定UI支持的屏幕方向,但有没有一种方法可以强制只允许一种方向?
谢谢。
在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];
}
}
UIDeviceOrientation
来设置setStatusBarOrientation
。根据class reference所述,您需要使用UIInterfaceOrientation
。但也许您可以解释一下您的理由? - JohnK我用这个方法成功了:
if (self.interfaceOrientation != UIInterfaceOrientationPortrait) {
// https://dev59.com/KnVC5IYBdhLWcg3wykej
// 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];
}
[[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(__bridge id)((void*)UIInterfaceOrientationPortrait)];
不过,在iOS6下这种方法无效。 - Hermann Klecker这个方法被称为确定您的界面是否应自动旋转到给定的旋转方向(即让UIKit来完成困难的工作,而不是手动完成)。
因此,如果您希望您的应用程序仅在横向模式下运行,则可以使用以下代码实现该方法:
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
如果您希望您的UI自动旋转到所有方向,则只需使用以下代码:
return YES;
这是您所询问的内容吗?
-shouldAutorotateToInterfaceOrientation:
方法并且只支持一个方向的模态视图控制器,整个界面将会自动强制进入该方向。我认为没有其他编程方式可以改变方向。在Swift中将方向改为竖屏:
UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
回到之前的页面:
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
//-----------------------------------
// 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];
如在相关主题中建议的那样, shouldAutorotateToInterfaceOrientation
已被弃用。新的应用程序应该利用以下方法,如苹果公司的UIViewController Class Reference所述,直到它们自己变得过时:
shouldAutorotate
,preferredInterfaceOrientationForPresentation
; 和attemptRotationToDeviceOrientation
。据我所知,最好为需要锁定(即具有首选)方向模式与上一个不同的视图使用不同的视图控制器。
虽然这不是一个理想的解决方案,但对我来说很有效。
- (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);
}
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeLeft );
}
shouldAutorotateToInterfaceOrientation
在 iOS 6.0 中已被弃用。请参见此处。 - JohnK