如何使用Cocoa-Touch禁用屏幕旋转?

时间:2014-10-06 15:47:12

标签: ios objective-c cocoa-touch

我无法通过以下方式禁用屏幕旋转:

 - (BOOL)shouldAutorotate {
     return NO;
   }
只有当我从项目的常规信息中禁用它时,才会禁用屏幕旋转 但是我不会从那里禁用它,这是我的代码(不是很多):

#import "MainPage.h"
#import "Reachability.h"
@interface MainPage ()



@end


@implementation MainPage
Reachability *internetReachableFoo;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidLoad];
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        CGSize result = [[UIScreen mainScreen] bounds].size;
        if(result.height == 480)
        {
            background.frame = CGRectMake(0, 0, 320, 480);
            about.frame = CGRectMake(20, 401, 280, 44);
        }
    }
    NSUserDefaults *userdetails =[NSUserDefaults standardUserDefaults];
    NSString *userid = [userdetails objectForKey:@"userid"];
    NSString *username = [userdetails objectForKey:@"username"];
    if (userid != NULL && username != NULL) {
       [self performSegueWithIdentifier:@"Home" sender:self];
        NSLog(@"connceted");
    }
    else
    {
        signOutlet.hidden = NO;
        loginOutlet.hidden = NO;
    }

}

- (BOOL)shouldAutorotate {
    return NO;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

我正在使用IOS 8和IOS 7吗? * edit *

好吧,我没有在其他VC中获得它的工作并且无法轮换 这是我的另一个VC(我没有看到任何变化)

#import "About.h"

@interface About ()

@end

@implementation About

- (void)viewDidLoad {

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        CGSize result = [[UIScreen mainScreen] bounds].size;
        if(result.height == 480)
        {
            background.frame = CGRectMake(0, 0, 320, 480);
            aboutText.frame = CGRectMake(15, 68, 290, 319);
            home.frame = CGRectMake(67, 414, 187, 38);
        }
    }

    [super viewDidLoad];
    // Do any additional setup after loading the view.

}

- (BOOL)shouldAutorotate {
    return NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

1 个答案:

答案 0 :(得分:0)

您需要在视图控制器中包含supportedInterfaceOrientations方法以及shouldAutorotate方法。因此,假设您希望这个特定的视图控制器仅限于纵向方向,您将拥有:

#import "MyViewController.h"

@interface MyViewController ()

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //...
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    //...
}


- (NSUInteger)supportedInterfaceOrientations {
    // support only portrait landscape
    return UIInterfaceOrientationMaskPortrait;
}


- (BOOL) shouldAutorotate {
    // have autorotate set to YES so the screen can rotate if not in Portrait orientation
    return YES;
}


- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    // not necessary, but this makes the view start in Portrait orientation on the initial load
    return UIInterfaceOrientationPortrait;
}

@end

希望这有帮助