xcode - 如何将视图锁定为纵向模式,但仍允许旋转一个视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15110838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
xcode - How do I keep views locked into portrait mode, but still allow one view to rotate?
提问by GrymHyman
I'm having a problem with device rotation. Except for ONE view, where I show a company splash screen, I want to lock all the remaining app views into Portrait display. In the project settings the supported orientations are Portrait and LandscapeLeft. In the 'Company Splash' it works fine and the view rotation is locked into LandscapeLeft no matter how I rotate the device. On all the other views when I rotate the device to the left the view alters instead of staying in portrait display. The methods are not even firing? If I remove Landscape left from the supported Orientations in the project, that screws up the 'Company Splash' view. I tried changing the shouldAutorotate
return to NO
, but that didn't help. Tried to work my way through the suggestions posted here, but that didn't help. If I put the following code into my AppDelegate.m, everything is locked into portrait mode and the 'Company Splash' crashes when accessed.
我遇到了设备旋转问题。除了 ONE 视图显示公司启动画面外,我想将所有剩余的应用程序视图锁定为纵向显示。在项目设置中,支持的方向是 Portrait 和 LandscapeLeft。在“公司飞溅”中,它工作正常,无论我如何旋转设备,视图旋转都会锁定在 LandscapeLeft 中。在所有其他视图中,当我将设备向左旋转时,视图会改变,而不是保持纵向显示。连开火的方法都没有?如果我从项目中支持的方向中删除横向左侧,则会破坏“公司飞溅”视图。我尝试将shouldAutorotate
return更改为NO
,但这没有帮助。试图按照我的方式解决这里发布的建议,但这没有帮助。如果我将以下代码放入我的 AppDelegate.m 中,则所有内容都被锁定为纵向模式,并且在访问时“Company Splash”会崩溃。
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
How do I lock the view into portrait mode no matter how the device is rotated except for the one screen?
除了一个屏幕之外,无论设备如何旋转,如何将视图锁定为纵向模式?
** method from the 'Company Splash' view. Again, works like it is supposed to.
** 来自“公司宣传”视图的方法。再次,像它应该的那样工作。
-(NSInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeLeft;
}
** methods from all the other views, which rotate out of portrait when I don't want them to
** 来自所有其他视图的方法,当我不希望它们旋转时
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// IOS5 Only returning that it should rotate to potrait
return (interfaceOrientation == UIDeviceOrientationPortrait);
}
-(BOOL)shouldAutorotate
{
// forcing the rotate IOS6 Only
return YES;
}
-(NSInteger)supportedInterfaceOrientations
{
// return number or enum IOS6 Only
return UIInterfaceOrientationMaskPortrait;
}
I thought maybe it might be because a UITabBarController is the root controller and I'm in a ViewController off of that? The methods are not even firing?
我想可能是因为 UITabBarController 是根控制器,而我在 ViewController 中?连开火的方法都没有?
回答by Ushan87
Add an observer to the viewDidLoad method of the view you want to rotate like this :
将观察者添加到要旋转的视图的 viewDidLoad 方法中,如下所示:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];
and then set the views according the the landscape view inside the orientationChangedmethod like this :
然后根据orientationChanged方法中的横向视图设置视图,如下所示:
- (void) orientationChanged:(NSNotification *)note{
UIDevice * device = [UIDevice currentDevice];
switch(device.orientation)
{
case UIDeviceOrientationPortrait:
break;
case UIDeviceOrientationPortraitUpsideDown:
break;
case UIDeviceOrientationLandscapeLeft:
break;
case UIDeviceOrientationLandscapeRight:
break;
default:
break;
};
}
回答by TomG103
Slap this method into the view controller you want locked in a certain orientation.
将此方法放入您想要锁定在某个方向的视图控制器中。
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
just change to the orientation you want (the above locks it in landscape)
只需更改为您想要的方向(以上将其锁定为横向)
回答by Arthur
maybe u should allow all orientations, and lock portrait orientation in each class except company splash with
也许你应该允许所有方向,并在每个班级中锁定纵向方向,除了公司飞溅
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
} else {
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
}