【问题标题】:force landscape ios 7强制景观ios 7
【发布时间】:2013-10-06 08:55:29
【问题描述】:

我尝试了以下方法来强制景观在我的观点上:

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeLeft;
}

- (BOOL)shouldAutorotate{
    return YES;
}

都没有工作。请注意,我正在模拟器和 iPad 上进行测试。

谢谢

【问题讨论】:

    标签: ios ios7 landscape viewcontroller


    【解决方案1】:

    以下是我如何使用 NavigationViewController 强制我的一个视图为横向:

    1. 实现了这个答案:https://stackoverflow.com/a/12662433/2394787

    2. 视图控制器中的导入消息:objc/message.h

    3. 在viewDidLoad方法中添加了这行代码:

    objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeLeft);

    希望对某人有所帮助。

    【讨论】:

    • 这个逻辑会被苹果批准吗?
    • 看来我只需要第 2 步和第 3 步(1 似乎是可选的)
    • 第 1 步可以防止用户再次进入纵向
    • 我让它在两个应用程序中运行
    • 这似乎在 iOS 模拟器中有效,但在实际设备上无效?
    【解决方案2】:

    接受的答案似乎不适用于 iOS 7.1.2。 (它适用于模拟器,但在设备上运行时不起作用。)

    这似乎可行:

    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];
    
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeLeft] forKey:@"orientation"];
    

    【讨论】:

    • 我的解决方案适用于装有 iOS 7.1.2 的 iPhone 4。你的设备是什么?
    【解决方案3】:
    int shouldShowInLandscape = 0;
    
    -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(forceToLandscape:)
                                                     name:@"yourNameNotification"
                                                   object:nil];
    
    }
    
    -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
    
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        {
                  return UIInterfaceOrientationMaskLandscapeLeft;
        } else {
                if(shouldShowInLandscape)
                {
                    return UIInterfaceOrientationMaskLandscape;
                }
                return UIInterfaceOrientationMaskPortrait;
        }
    }
    
    -(void) forceToLandscape:(NSNotification*) theNot
    {
        shouldShowInLandscape = [[theNot object] intValue];
    
    }
    

    //来自 UIViewController

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        [[NSNotificationCenter defaultCenter]
         postNotificationName:@"yourNameNotification"
         object:[NSString stringWithFormat:@"1"]];
    }
    
    -(void) viewDidDisappear:(BOOL)animated
    {
    
            [[NSNotificationCenter defaultCenter]
         postNotificationName:@"yourNameNotification"
         object:[NSString stringWithFormat:@"0"]]; 
    
    }
    

    // 移除你的通知中心

    【讨论】:

      猜你喜欢
      • 2014-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多