自动旋转锁定,以编程方式

发布于 2024-11-09 08:57:54 字数 191 浏览 0 评论 0原文

有谁知道是否有可能以编程方式锁定 iPhone 的自动旋转,仅用于一个视图? 我想对半半透明视图提供某种帮助,但我想仅支持横向,即使所有其他视图都可以旋转。 所以我希望当该视图位于顶部时锁定旋转。

tnx

编辑:更多详细信息:一个 UIController 有 7 个 UIView...并且我希望在最后一个出现在顶部时锁定自动旋转。

Does anybody know if there is a possibility to lock autorotation of iPhone programmatically for just one view?
I want to make some kind of help with semi-transculent view, but I want to support only landscape orientation even all other views can rotate.
So I wish to lock rotation when this view is on the top.

tnx

EDIT: More details: one UIController has 7 UIView...and I wish to lock the autorotation just when the last one occurs on the top.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

别挽留 2024-11-16 08:57:54

这个问题是一年多前提出的,但接受的方法现在已被弃用。在 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;
}

This question was asked over a year ago but the accepted method is now deprecated in iOS 6 so if anybody is interested in doing this in iOS 6 then you need to use supportedInterfaceOrientations on the topmost controller.

Imagine you have a setup like this...

  • Tab Bar Controller
    • Navigation Controller
      • View Controller

... then you need to set the supportedInterfaceOrientations method on the tab bar controller.

Create a subclass of the tab bar controller (or navigation controller if that is at the top) and set these methods in it...

- (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;
}

... then in your individual view controllers you can set the properties you want...

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations{
    //return supported orientation masks
    return UIInterfaceOrientationMaskLandscape;
}
沫雨熙 2024-11-16 08:57:54

使用以下...

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

Use the following...

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
奢华的一滴泪 2024-11-16 08:57:54

您可以将其贴在窗户上。加载视图后,

[self.view.window addSubview:yourStaticView];
[self.view.window bringSubviewToFront:yourStaticView]; // Do only if necessary

离开此视图时将其删除。可能在 viewWillDisappear:viewDidDisappear: 中。

You can attach it to the window. After the view is loaded, do

[self.view.window addSubview:yourStaticView];
[self.view.window bringSubviewToFront:yourStaticView]; // Do only if necessary

Remove it when leaving this view. Probably in viewWillDisappear: or viewDidDisappear:.

梦行七里 2024-11-16 08:57:54
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    //If you don't want to support multiple orientations uncomment the line below
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
    //return [super shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    //If you don't want to support multiple orientations uncomment the line below
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
    //return [super shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}
默嘫て 2024-11-16 08:57:54

我觉得有很多具体的答案可以间歇性地工作,但是没有一个可以深入了解如果用户开始将手机倾斜到视图控制器之外,相对于应用程序的其余部分或其他视图控制器而言,后果或副作用是什么您想控制方向...

在使用它之后,您可能会意识到(像我自己一样)可能会出现不利或不期望的结果(即,当您不希望发生方向变化时发生,反之亦然)。

我的主要认识是只有“根视图控制器”才会调用“shouldAutorotate”,而不仅仅是您尝试覆盖的任何单个视图控制器。

有了这种认识,似乎很难“锁定”特定视图控制器的特定方向。

(意味着vc_A始终为纵向并且不允许更改为横向,而vc_B始终为横向并且不允许更改为纵向)

在确认这一点后,以下算法这对我有用,只能在指定的视图控制器上旋转。

设置:

首先,您必须在 info.plist 或主项目设置文件中允许您想要的方向(这些方向将是您可以在代码中使用的唯一方向)

在此处输入图像描述

代码:

1) 在我的根视图控制器(此处:MasterViewController)中我指定了一个 BOOL 属性(允许自动旋转)当调用“shouldAutorotate”时将使用它。

2)还使根视图控制器成为单例,以便可以从任何其他子视图控制器轻松访问它(无需传递引用)。

注意:您还可以使用观察者/通知模式或委托或其他模式,但对我来说,单例模式是最简单的

3) 添加委托“-(BOOL)shouldAutorotate”并利用 BOOL allowedAutorotate 来返回

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;”

因此,我们的结果是只在我们想要的时候以编程方式允许/执行方向改变一次。

GLHF!

i feel there are many specific answers that work intermittently, but none provide insight as to what the ramifications or side effects are, in respect to the rest of the app or other view controllers, if the user starts tilting the phone outside of the view controller you want to control the orientation for...

after playing around with it, you may realize (like myself) that adverse or undesired results may occur (ie. orientation changes occur when you don't want them to, or vice versa).

my main realization involved that only the 'root view controller' will invoke 'shouldAutorotate', and NOT just any individual view controller you attempt to override with.

with this realization it seemed quite difficult to 'lock' a specific orientation for a specific view controller.

(meaning have vc_A always be portrait and not allowed to change to landscape, while having vc_B always be landscape and not allowed to change to portrait)

after acknowledging this, the following algorithm is what worked for me in being able to only rotate on specified view controllers.

setup:

first you have to allow the orientations you desire, in either the info.plist or the main project settings file (these orientations will be the only ones you can use in your code)

enter image description here

code:

1) in my root view controller (here: MasterViewController) i designated a BOOL property (allowAutorotate) that will be utilized when 'shouldAutorotate' is invoked.

2) also make the root view controller a singleton so its easily accessible from any other child view controller (without having to pass around references).

note: you may also use the observer/notification pattern or delegation or some other pattern, but for me the singleton pattern was easiest

3) add the delegate '-(BOOL)shouldAutorotate' and utilize the BOOL allowAutorotate for its return

4) create an instance method 'setInterfaceOrientation'. some other class will call this method in their 'viewDidLoad' and/or in their '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) finally in some other class get the root view controller and invoke 'setInterfaceOrientation' accordingly

// 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];
}

notes:

1) the result of this example should be that the app will initially load in portrait, then when you load 'SomeViewController', it will change to landscape, and then when you remove it, it will change back to portrait.

2) it works like this...

every time you physically tilt the phone, the delegate 'shouldAutorotate' is invoked (only from the 'root view controller'),

as well every time you programmatically tilt the phone

NSNumber *value = [NSNumber numberWithInt:orientation];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];

the delegate 'shouldAutorotate' is invoked.

this is why we first 'allowAutorotate = YES;', then 'tilt the phone', then 'allowAutorotate = NO;'

hence, we have a result of only allowing/performing the orientation change once, programmatically, exactly when we want to.

glhf!

不再让梦枯萎 2024-11-16 08:57:54
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {                  
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight);  
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {                  
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight);  
}
茶底世界 2024-11-16 08:57:54

我找到了非常简单且有效的解决方案。通过添加顶视图,我设置了一个 BOOL,然后控制旋转。

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations; we support all.

    if (helpViewOnTheTop) {
        return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
    }
    else
    {
        return YES;
    }
}


I have found very simple and working solution. By adding the top view I set a BOOL which then controls rotating.

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations; we support all.

    if (helpViewOnTheTop) {
        return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
    }
    else
    {
        return YES;
    }
}


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文