限制某些视图的自转 [英] restricting autorotation on certain views
问题描述
我的应用程序包含两个表视图控制器.在第一个中,我希望视图能够左右旋转(除了纵向模式),但是在第二个表视图控制器中(我在点击第一个表中的单元格后导航到)我想要它只能在纵向模式下查看.我试过这段代码,但没有用,它一直在旋转.
my app contains two table view controllers. in the first one i want the view to be able to be rotated left and right (in addition to the portrait mode), however in the second table view controller ( which i navigate to after tapping a cell from the first table) i want it to only be viewed in portrait mode. i tried this code but it didn't work, it keeps on rotating.
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL) shouldAutorotate {
return NO;
}
注意:我确实从项目目标的摘要"选项卡中启用了左/右/纵向方向.有什么解决办法吗?
note: i did actually enabled the left/right/portrait orientation from the Summary tab of the project target. any fix?
推荐答案
为 UINavigationController 创建一个类别,其中包括以下方法:
Create a category for UINavigationController which include the following methods:
(适用于 iOS 6 和 iOS 5)
(Works both for iOS 6 and iOS 5)
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return [self.topViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}
然后在你的控制器中实现这些方法
Then implement these methods in your controllers
首先:
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
if (RUNNING_IPAD) {
return UIInterfaceOrientationMaskAll;
}
else {
return UIInterfaceOrientationMaskAllButUpsideDown;
};
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if (RUNNING_IPAD) {
return YES;
}
else {
return toInterfaceOrientation != UIInterfaceOrientationMaskPortraitUpsideDown;
}
}
其次:
- (BOOL)shouldAutorotate {
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return NO;
}
项目的旋转设置应如下所示:
The rotation settings of the project should look like:
这篇关于限制某些视图的自转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!