10

我尝试了以下方法来强制我的观点中的一个景观:

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeLeft;
}

- (BOOL)shouldAutorotate{
    return YES;
}

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

谢谢

4

3 回答 3

22

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

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

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

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

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

希望它可以帮助某人。

于 2013-10-01T11:11:41.093 回答
7

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

这似乎有效:

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

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeLeft] forKey:@"orientation"];
于 2014-08-01T13:24:56.207 回答
1
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-08-09T00:05:41.013 回答