IOS,我可以在一些viewController中强制定位

时间:2015-10-08 05:16:49

标签: ios xcode orientation viewcontroller

Pic from my Project

我在一般设置中设置纵向方向 但我想强制一些视图控制器设置为横向方向 我可以这样做吗?

3 个答案:

答案 0 :(得分:0)

是的,您可以强制查看控制器的方向。

例如

......

class ViewController: UIViewController {
    override func supportedInterfaceOrientations() -> Int {
         return Int(UIInterfaceOrientationMask.Landscape.rawValue)
    }
}

答案 1 :(得分:0)

您可以在app委托中添加此方法 示例: -

- (NSUInteger)应用程序:(UIApplication *)应用程序    supportedInterfaceOrientationsForWindow:(UIWindow *)窗口 { if([self.window.rootViewController.presentedViewController isKindOfClass:[SecondViewController class]])     {         SecondViewController * secondController =(SecondViewController *)self.window.rootViewController.presentedViewController;

    if (secondController.isPresented)
        return UIInterfaceOrientationMaskAll;
    else return UIInterfaceOrientationMaskPortrait;
}
else return UIInterfaceOrientationMaskPortrait;

}

更多细节请参考链接: - > http://swiftiostutorials.com/ios-orientations-landscape-orientation-one-view-controller/

答案 2 :(得分:0)

您可以在ViewController中尝试使用此代码,您希望它处于横向模式。另外,请不要忘记在支持的界面方向中在info.plist中添加横向模式。

- (void)viewWillAppear:(BOOL)animated{
BOOL pushed = [self isMovingToParentViewController];

if (pushed && [[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
{
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}}

- (void)viewWillDisappear:(BOOL)animated{
BOOL popped = [self isMovingFromParentViewController];

if (popped && [[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]){
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}}