Iphone 如何在ios 7中阻止旋转

Iphone 如何在ios 7中阻止旋转,iphone,objective-c,uiview,rotation,Iphone,Objective C,Uiview,Rotation,在ios 7之前,我使用这段代码来阻止旋转(我也使用了xibs,现在是故事板) 现在我迁移到storyboard和ios7,它不工作了,我的视图仍然在旋转 更新: 我通过将此代码添加到委托中解决了这个问题,现在我以前的代码工作得很好 - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ NSUInteger orient

在ios 7之前,我使用这段代码来阻止旋转(我也使用了xibs,现在是故事板)

现在我迁移到storyboard和ios7,它不工作了,我的视图仍然在旋转

更新: 我通过将此代码添加到委托中解决了这个问题,现在我以前的代码工作得很好

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
 NSUInteger orientations = UIInterfaceOrientationMaskPortrait;
 if (self.fullScreenVideoIsPlaying) {
     return UIInterfaceOrientationMaskAllButUpsideDown;
 }
 else {        
     if(self.window.rootViewController){
         UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
         orientations = [presentedViewController supportedInterfaceOrientations];
 }


return orientations;
  }

如果你根本不想让你的应用程序旋转(无论哪个视图处于活动状态),你可以在Xcode侧栏中点击你的项目,向下滚动,然后取消选择左横向和右横向


在iOS7开发所需的XCode 5中,您可以转到目标位置,并在“部署信息”下取消选中除“纵向”以外的所有选项,以确定设备方向


如果您只想在横向模式下使用,那么可以使用xcode项目设置来实现 转到目标>摘要>支持界面方向

或者你可以做一个代码

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (UIInterfaceOrientationIsLandscape(interfaceOrientation));
}

阿特里克的密码起作用了。这里有一个更完整的解决方案,它只允许锁定和解锁纵向模式,即使使用UINavigationController

appdelegate .h


@property (nonatomic) BOOL screenIsPortraitOnly;


appdelegate .m

#pragma mark - View Orientation

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    NSUInteger orientations = UIInterfaceOrientationMaskPortrait;
    if (self.screenIsPortraitOnly) {
        return UIInterfaceOrientationMaskPortrait;
    }
    else {
        if(self.window.rootViewController){
            UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
            orientations = [presentedViewController supportedInterfaceOrientations];
        }
        return orientations;
    }
}
对于需要纵向锁定的所有视图控制器 如果您没有使用导入了app委托的子类,那么不要忘记导入委托。对于大多数视图控制器,我使用UIViewController的子类,它至少执行导入操作

#import "AppDelegate.h"
我将其用于所有纵向锁定的ViewController

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:true];
    [self portraitLock];
}

-(void) portraitLock {
    AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
    appDelegate.screenIsPortraitOnly = true;
}

#pragma mark - interface posiiton

- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL) shouldAutorotate {
    return NO;
}
ViewDidLoad在ViewDid出现之前运行,因此我在UIViewController的子类中运行它来解锁所有屏幕。视图将以锁定方法显示,仅在我需要锁定屏幕的屏幕中使用

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self portraitUnLock];
}

-(void) portraitUnLock {
    AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
    appDelegate.screenIsPortraitOnly = false;
}

我不能这样做。单击“向左横向”时,复选框将关闭,然后立即重新打开。另外,如果我转到Info.plist并删除横向方向,它会将它们放回原处。
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self portraitUnLock];
}

-(void) portraitUnLock {
    AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
    appDelegate.screenIsPortraitOnly = false;
}