Objective C只在viewcontroller中纵向定位

时间:2014-01-15 08:38:46

标签: ios iphone objective-c ipad

我只需要在纵向方向上拥有一个ViewController。我尝试使用此代码,但它不起作用:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL) shouldAutorotate {
    return NO;
}

我也试过了:

AppDelegate.m:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    NSLog(@"Interface orientations %@", window);
    if(!enablePortrait)
        return UIInterfaceOrientationMaskLandscape|UIInterfaceOrientationMaskPortrait;
    return UIInterfaceOrientationMaskPortrait;
}

AudioViewController.m

- (void) viewWillAppear:(BOOL)animated{

    [super viewWillAppear:YES];

    ((AppDelegate *)[[UIApplication sharedApplication] delegate]).enablePortrait= YES;
}

如果最初iPad处于纵向模式,它会起作用。但如果iPad处于横向模式,当我回到之前的viewController(AudioViewController.m)时,viewController也会保持在横向状态。

我也试过了,我查看了WillAppear.m但没有效果:

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];

请帮助我!

感谢您提前。

修改

临时解决方案:

最后我这样做:(它对我有用,但我希望找到更好的解决方案)

- (void) viewWillAppear:(BOOL)animated{

    [super viewWillAppear:YES];

    ((AppDelegate *)[[UIApplication sharedApplication] delegate]).enablePortrait= YES;
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];

    UINavigationController* nc = [[UINavigationController alloc] init];
    [self.navigationController presentViewController:nc animated:NO completion:^{
    }];
    [self.navigationController dismissViewControllerAnimated:YES completion:^{
    }];
}

4 个答案:

答案 0 :(得分:0)

-(BOOL)shouldAutorotate{
    if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {
        return YES;
    }
    else {
        return NO;
    }
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

请将您的supportedInterfaceOrientations方法设为相同。如果有帮助,请告诉我.. :)

答案 1 :(得分:0)

在APPDelegate类中使用它来使viewcontroller始终处于纵向:- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { return UIInterfaceOrientationMaskPortrait; }

答案 2 :(得分:0)

答案 3 :(得分:0)

最后我这样做:(它对我有用,但我希望找到更好的解决方案)

- (void) viewWillAppear:(BOOL)animated{

    [super viewWillAppear:YES];

    ((AppDelegate *)[[UIApplication sharedApplication] delegate]).enablePortrait= YES;
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];

    UINavigationController* nc = [[UINavigationController alloc] init];
    [self.navigationController presentViewController:nc animated:NO completion:^{
    }];
    [self.navigationController dismissViewControllerAnimated:YES completion:^{
    }];
}