首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >强制横向ios 7

强制横向ios 7
EN

Stack Overflow用户
提问于 2013-09-30 21:11:41
回答 3查看 14.9K关注 0票数 10

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

代码语言:javascript
复制
- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeLeft;
}

- (BOOL)shouldAutorotate{
    return YES;
}

两种方法都不起作用。请注意,我是在模拟器和iPad上测试的。

谢谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-10-01 19:11:41

以下是我是如何使用NavigationViewController强制我的一个视图成为景观的:

视图控制器中的https://stackoverflow.com/a/12662433/2394787

  • Imported消息:在viewDidLoad方法中objc/message.h

  • Added这行代码:

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

希望这能帮助到别人。

票数 22
EN

Stack Overflow用户

发布于 2014-08-01 21:24:56

公认的答案似乎在iOS 7.1.2上不起作用。(它在模拟器上工作,但在设备上运行时不工作。)

不过,这似乎是可行的:

代码语言:javascript
复制
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeLeft] forKey:@"orientation"];
票数 7
EN

Stack Overflow用户

发布于 2014-08-09 08:05:41

代码语言:javascript
复制
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

代码语言:javascript
复制
- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"yourNameNotification"
     object:[NSString stringWithFormat:@"1"]];
}

-(void) viewDidDisappear:(BOOL)animated
{

        [[NSNotificationCenter defaultCenter]
     postNotificationName:@"yourNameNotification"
     object:[NSString stringWithFormat:@"0"]]; 

}

//移除你的notificationCenter

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19095161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档