2013-10-09 37 views
2

我用這個代碼來阻止IOS 7之前的旋轉旋轉(我還使用xibs,現在故事板)如何阻止IOS 7

- (BOOL)shouldAutorotate { 
    return NO; 
} 

- (NSUInteger)supportedInterfaceOrientations { 
return UIInterfaceOrientationPortrait; 
} 

現在,我遷移到故事情節和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; 
    } 

回答

0

如果您不希望您的應用程序在所有的旋轉(無論哪個觀點是活動),您可以在Xcode側邊欄中單擊您的項目,向下滾動,然後取消選擇Landscape Left和Landscape Right。

2

在Xcode 5,這是需要用於iOS7的發展,你可以去你的目標,並在部署信息取消除傾情爲設備方向的一切。

portrait only

+0

我無法做到這一點。當我點擊風景左側時,複選框會關閉,然後立即重新打開。另外,如果我轉到Info.plist並刪除橫向方向,它會將其放回。 – Almo

3

如果你只想在橫向模式下,你可以使用Xcode項目設置 去TARGET>摘要>支持的接口方向做

enter image description here

或者你也可以做一個代碼

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

Atrik的代碼工作。這裏是一個更完整的解決方案,它允許鎖定和解鎖肖像模式,僅即使使用的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; 
    } 
} 

的對,我需要所有的肖像視圖控制器鎖定 如果你還沒有使用具有一個子類導入的應用程序委託,然後不要忘記導入委託。對於大多數視圖控制器,我使用至少可以導入的UIViewController的子類。

#import "AppDelegate.h" 

我用這個爲所有肖像鎖定視圖控制器。

-(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中viewDidAppear之前運行,所以我在我的UIViewController子類運行這個解鎖所有屏幕。 viewWillAppear與鎖定方法僅用於我需要鎖定屏幕的cotrollers。

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

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