【发布时间】:2011-05-24 09:13:30
【问题描述】:
有谁知道是否有可能以编程方式锁定 iPhone 的自动旋转以仅用于一个视图? 我想对半透明视图提供某种帮助,但我只想支持横向,即使所有其他视图都可以旋转。 所以我希望当这个视图在顶部时锁定旋转。
tnx
编辑:更多细节:一个 UIController 有 7 个 UIView...我希望在最后一个出现在顶部时锁定自动旋转。
【问题讨论】:
标签: iphone objective-c
有谁知道是否有可能以编程方式锁定 iPhone 的自动旋转以仅用于一个视图? 我想对半透明视图提供某种帮助,但我只想支持横向,即使所有其他视图都可以旋转。 所以我希望当这个视图在顶部时锁定旋转。
tnx
编辑:更多细节:一个 UIController 有 7 个 UIView...我希望在最后一个出现在顶部时锁定自动旋转。
【问题讨论】:
标签: iphone objective-c
这个问题是在一年前提出的,但现在接受的方法是deprecated in iOS 6,所以如果有人有兴趣在 iOS 6 中这样做,那么您需要在最顶层的控制器上使用 supportedInterfaceOrientations。
想象一下你有这样的设置......
...那么你需要在标签栏控制器上设置supportedInterfaceOrientations方法。
创建标签栏控制器的子类(或导航控制器,如果它在顶部)并在其中设置这些方法...
- (BOOL)shouldAutorotate {
//Use this if your root controller is a navigation controller
return self.visibleViewController.shouldAutorotate;
//Use this if your root controller is a tab bar controller
return self.selectedViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations {
//Navigation Controller
return self.visibleViewController.supportedInterfaceOrientations;
//Tab Bar Controller
return self.selectedViewController.supportedInterfaceOrientations;
}
...然后在您的个人视图控制器中,您可以设置您想要的属性...
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations{
//return supported orientation masks
return UIInterfaceOrientationMaskLandscape;
}
【讨论】:
使用以下...
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
【讨论】:
您可以将其附加到窗口。加载视图后,执行
[self.view.window addSubview:yourStaticView];
[self.view.window bringSubviewToFront:yourStaticView]; // Do only if necessary
离开此视图时将其删除。可能在viewWillDisappear: 或viewDidDisappear:。
【讨论】:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
//If you don't want to support multiple orientations uncomment the line below
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
//return [super shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}
【讨论】:
我觉得有许多具体的答案会间歇性地起作用,但没有一个可以深入了解如果用户开始将手机倾斜到外面您要控制其方向的视图控制器...
在玩弄它之后,您可能会意识到(像我一样)可能会出现不利或不希望的结果(即,当您不希望它们发生方向变化时,反之亦然)。
我的主要认识涉及只有“根视图控制器”会调用“shouldAutorotate”,而不仅仅是您尝试覆盖的任何单个视图控制器。
有了这种认识,似乎很难为特定视图控制器“锁定”特定方向。
(意思是vc_A始终是纵向,不允许改成横向,而vc_B始终是横向,不允许改成纵向)
在确认这一点后,以下算法对我来说是可行的,因为它只能在指定的视图控制器上旋转。
设置:
首先,您必须在 info.plist 或主项目设置文件中允许您想要的方向(这些方向将是您可以在代码中使用的唯一方向)
代码:
1) 在我的根视图控制器(此处:MasterViewController)中,我指定了一个 BOOL 属性(allowAutorotate),当调用“shouldAutorotate”时将使用该属性。
2) 还使根视图控制器成为单例,因此可以从任何其他子视图控制器轻松访问它(无需传递引用)。
注意:您也可以使用观察者/通知模式或委托或其他模式,但对我来说单例模式最简单
3) 添加委托 '-(BOOL)shouldAutorotate' 并利用 BOOL allowAutorotate 返回
4) 创建一个实例方法'setInterfaceOrientation'。其他一些类会在他们的“viewDidLoad”和/或他们的“viewWillDisappear”中调用这个方法
// 1)
@implementation MasterViewController {
BOOL allowAutorotate;
}
// 2)
+ (id)sharedMasterViewController {
static MasterViewController *sharedMasterViewController = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMasterViewController = [[self alloc] init];
});
return sharedMasterViewController;
}
- (id)init
{
self = [super init];
if (self)
{
allowAutorotate = NO;
}
return self;
}
// 3)
- (BOOL)shouldAutorotate
{
return allowAutorotate;
}
// 4)
- (void)setInterfaceOrientation:(NSInteger)orientation
{
allowAutorotate = YES;
NSNumber *value = [NSNumber numberWithInt:orientation];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
allowAutorotate = NO;
}
5) 最后在其他类中获取根视图控制器并相应地调用“setInterfaceOrientation”
// 5)
#import "MasterViewController.h"
@implementation SomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[[MasterViewController sharedMasterViewController] setInterfaceOrientation:UIInterfaceOrientationLandscapeRight];
}
- (void)viewWillDisappear:(BOOL)animated
{
[[MasterViewController sharedMasterViewController] setInterfaceOrientation:UIDeviceOrientationPortrait];
}
注意事项:
1) 这个例子的结果应该是应用程序最初会以纵向加载,然后当你加载'SomeViewController'时它会变成横向,然后当你删除它时它会变回纵向。
2) 它是这样工作的......
每次您物理倾斜手机时,都会调用委托“shouldAutorotate”(仅来自“根视图控制器”),
以及每次以编程方式倾斜手机时
NSNumber *value = [NSNumber numberWithInt:orientation];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
委托“shouldAutorotate”被调用。
这就是为什么我们首先'allowAutorotate = YES;',然后'倾斜手机',然后'allowAutorotate = NO;'
因此,我们只允许/执行一次方向更改,以编程方式,恰好在我们想要的时候。
咯咯!
【讨论】:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
【讨论】:
我找到了非常简单且有效的解决方案。通过添加顶视图,我设置了一个 BOOL,然后控制旋转。
// 覆盖以允许默认纵向以外的方向。 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // 对于支持的方向返回 YES;我们都支持。 if (helpViewOnTheTop) { 返回 (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight); } 别的 { 返回是; } }【讨论】: