我如何仅针对iOS 15.4.1的某个ViewController强制取向?

发布于 2025-01-29 21:59:41 字数 879 浏览 3 评论 0原文

我有一个可以在iPad和iPhone上运行的应用程序。出于X原因,我需要为特定的ViewController进行硬码定向。我们还能在Objective-C中的iOS 15.4.1上这样做吗?我知道过去问这个问题,但我找不到任何答案,并且在以前的iOS版本中被问到。

我尝试了:

-(BOOL)shouldAutorotate {
    return YES;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

这是:

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

还有更多。到目前为止什么都没有。

谢谢。

I have an application that can run on both iPad and iPhone. I Need to hardcode the orientation for a specific viewController for x reason. Can we still do that on ios 15.4.1 in Objective-c? I Know this question have been asked in the past but i could not find any answer and they were asked in previous ios version.

I tried this :

-(BOOL)shouldAutorotate {
    return YES;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

And this :

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

And much more. nothing worked so far.

Thanks.

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

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

发布评论

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

评论(1

触ぅ动初心 2025-02-05 21:59:41

我找到了一个修复程序!一个奇怪的修复,但仍然如此。我认为这不是一个好方法,也不是那么强大,而是它目前的工作。

问题是,您无法设置设备方向forkey:@“ entrientation”
如果设备已经处于该位置(值不会更改)。因此,您的视图不会旋转(我的2分是在更改值时会有触发器)。

要解决此问题,您要么必须物理旋转设备(用户可能不知道这是他要做的),要么可以强制forkey的价值:@“ entrientation”才能更改,以便您可以再次更改它,然后设备设备然后将旋转。

这里是 :

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIDeviceOrientation currentDeviceOrientation = [[UIDevice currentDevice] orientation];
    if(currentDeviceOrientation == UIDeviceOrientationPortrait){
        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    }
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];

我发现它必须在ViewDidload功能中或无法使用。

简单,丑陋但有效。

I found a fix!! A weird fix, but still. I don't think this is the good way and not that robust but its working for now..

The problem is, you cant set the device orientation forKey:@"orientation"
if the device is already in that position (the value won't change). So your view won't rotate (My 2 cent guess is that there is a trigger when the value is changed).

To work around that, you either have to rotate the device physically (the user might not know that's what he have to do) OR you can force the value of forKey:@"orientation" to change so you can change it again and the device will then rotate.

Here it is :

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIDeviceOrientation currentDeviceOrientation = [[UIDevice currentDevice] orientation];
    if(currentDeviceOrientation == UIDeviceOrientationPortrait){
        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    }
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];

And I found out that it HAS to be in the viewDidLoad function or it wont work.

Simple, ugly, but efficient.

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