【问题标题】:How to disable specific orientation in iOS如何在 iOS 中禁用特定方向
【发布时间】:2015-04-21 05:54:20
【问题描述】:

我想在某些视图中禁用横向。

我已经重写了以下两个方法,但是这些方法不会随时调用。

- (NSUInteger) supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

我错过了什么吗? 请帮帮我。

【问题讨论】:

  • 你可以通过界面生成器来完成。
  • @DheerajSingh,这可以防止。如我所愿。但是如果我想在一些push viewController中支持横向呢?
  • @AshokLondhe 我提到,我只想在某些观点上阻止。通过界面生成器,我可以禁用全部或启用或。
  • 每个视图控制器都有不同的方向。您可以在界面生成器中使用它。每个 ViewController 都有 Orientation 属性,你可以手动设置。

标签: ios objective-c landscape-portrait orientation-changes


【解决方案1】:

创建 UINavigationController 的子类并按如下方式覆盖这些方法:

- (NSUInteger)supportedInterfaceOrientations
{
    if([self.topViewController respondsToSelector:@selector(supportedInterfaceOrientationsForThisContorller)])
    {
        return(NSInteger)[self.topViewController performSelector:@selector(supportedInterfaceOrientationsForThisContorller) withObject:nil];
    }
    return UIInterfaceOrientationPortrait | UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}

- (BOOL)shouldAutorotate
{
    if([self.visibleViewController respondsToSelector:@selector(shouldAutorotateNow)])
    {
        BOOL autoRotate = (BOOL)[self.visibleViewController
performSelector:@selector(shouldAutorotateNow)
                                 withObject:nil];
        return autoRotate;
    }
    return YES;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

并在您只想设置纵向的 UIViewController 类中使用以下方法

- (NSUInteger) supportedInterfaceOrientationsForThisContorller {
    // Return a bitmask of supported orientations. If you need more,
    // use bitwise or (see the commented return).
    return UIInterfaceOrientationMaskPortrait;
    // return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

-(BOOL)shouldAutorotateNow
{
    return YES;
}

【讨论】:

    【解决方案2】:

    查看this 答案。这家伙可能已经详细解释了代表。

    也试试,

    - (void)awakeFromNib
    {
        isShowingLandscapeView = NO;
        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(orientationChanged:)
                                     name:UIDeviceOrientationDidChangeNotification
                                     object:nil];
    }
    
    - (void)orientationChanged:(NSNotification *)notification
    {
        UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
        if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
            !isShowingLandscapeView)
        {
            [self performSegueWithIdentifier:@"DisplayAlternateView" sender:self];
            isShowingLandscapeView = YES;
        }
        else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&
                 isShowingLandscapeView)
        {
            [self dismissViewControllerAnimated:YES completion:nil];
            isShowingLandscapeView = NO;
        }
    }
    

    【讨论】:

    • 此方法在 iOS 6 中已弃用。所以我不能使用此方法
    • 方向改变时不调用此方法。
    • 是的,我正在使用通知来改变方向。虽然我可以检测到方向。但是我该如何预防呢??
    【解决方案3】:

    您是否在代码中使用了以下方法,没有此supportedInterfaceOrientations 将不会被调用

    -(BOOL) 应该自动旋转 {

    return YES; 
    

    }

    【讨论】:

    • 是的,是我写的
    • 好吧,如果您使用的是导航控制器,那么它必须工作,否则如果它在子视图内,则只有 root 会收到此方法调用。还要检查 info plist 中正确设置的方向支持。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    • 1970-01-01
    相关资源
    最近更新 更多