2014-12-01 60 views
2

我正在研究有大約12個視圖的應用程序。我希望有些人只是肖像而其他人是風景畫。Xcode IOS鎖一些方向肖像和一些風景

我該如何去鎖定一些視圖的方向到肖像和其他景觀?

我試着從前面的例子下面的代碼,但它不鎖定方向。

[[UIDevice currentDevice] setValue: 
[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeLeft] 
          forKey:@"orientation"]; 

回答

3

近24個小時的努力後,我終於解決了這一問題。對於一個「ShouldAutoRotate」永遠不會被調用。這就是我所能找到的所有地方「只需添加shouldAutoRotate」......沒有工作。另外我不確定爲什麼人們會投我的問題。

我的工作是將下面的方法添加到我的Appdelegate.m文件。

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ 

    NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown; 

    if(self.window.rootViewController){ 
     UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject]; 
     orientations = [presentedViewController supportedInterfaceOrientations]; 
    } 

    return orientations; 
} 

然後將下面的方法添加到viewcontroller.m文件我想鎖定到橫向。

- (NSUInteger)supportedInterfaceOrientations{ 
    return UIInterfaceOrientationMaskLandscape; 
} 
+0

如果您在構建設置中啓用其他界面方向,shouldAutoRotate應該可以工作。 – 2015-02-06 08:15:35

+0

完美答案.... – 2015-11-02 13:12:39

-1

你必須在你的ViewController中實現supportedInterfaceOrientationshouldAutorotate

Apple Documentation

+0

LucaD您好, 加入 - (NSUInteger)supportedInterfaceOrientations和或 - (BOOL)shouldAutorotate。視圖仍然旋轉。 取自範例http://chrisrisner.com/31-Days-of-iOS--Day-16%E2%80%93Handling-Device-Orientation – Mich 2014-12-01 20:46:46