如何只允许单个UIViewController在横向和纵向方向上旋转?

Roh*_*han 60 iphone objective-c uiviewcontroller ios

我的应用程序仅适用于iphone设备(iphone 4和5)并且仅支持ios 6.

我的整个应用程序只支持portrait模式.但是有一个名为" ChatView"的视图,我想支持它们landscapeportrait模式.

我已按如下方式设置所需的设备旋转 -

在此输入图像描述

我也尝试过以下代码来支持"ChatView"中的旋转 -

-(BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}
Run Code Online (Sandbox Code Playgroud)

但它无法旋转那个视图.

我已经搜索了很多,但无法找到我的问题的解决方案.

而且在"ChatView"中还有一些对象,如按钮,文本字段,其框架是以编程方式设置的.所以我想知道我是否还必须为横向模式设置所有这些对象的帧?

请帮我.

谢谢.....

Ala*_*977 63

简单,但它工作得很好.IOS 7.1和8

AppDelegate.h

@property () BOOL restrictRotation;
Run Code Online (Sandbox Code Playgroud)

AppDelegate.m

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if(self.restrictRotation)
    return UIInterfaceOrientationMaskPortrait;
else
    return UIInterfaceOrientationMaskAll;
}
Run Code Online (Sandbox Code Playgroud)

视图控制器

-(void) restrictRotation:(BOOL) restriction
{
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    appDelegate.restrictRotation = restriction;
}
Run Code Online (Sandbox Code Playgroud)

viewDidLoad中

[self restrictRotation:YES]; or NO
Run Code Online (Sandbox Code Playgroud)

  • [[UIDevice currentDevice] setValue:[NSNumbernumberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"]; 或者你想要什么. (3认同)
  • 从ViewController A转到ViewController B时可以使用,但是当用户从B(允许所有方向)返回到A(仅纵向)时,即使不应该以横向显示A。 (2认同)
  • 我很遗憾回答了你的问题(尽快).如果您无法自己找到解决方案!!!! 尽快贬义!!!!!!! (2认同)

Nig*_*ury 28

我想如果你只想支持一个viewcontroller旋转,那么因为应用程序将遵循你在.plist文件中设置的方向,所以不可能.您可以遵循的替代方法是支持横向和纵向的应用程序,将所有viewcontrollers旋转冻结为纵向(聊天视图除外).

编辑

要子类化UINavigationController,创建一个名为eg的新文件,CustomNavigationController并使其成为子类UINavigationController.

.h文件

#import <UIKit/UIKit.h>

@interface CustomNavigationController : UINavigationController

@end
Run Code Online (Sandbox Code Playgroud)

.m文件

#import "CustomNavigationController.h"

@interface CustomNavigationController ()

@end


@implementation CustomNavigationController

-(BOOL)shouldAutorotate
{
    return NO;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}

@end
Run Code Online (Sandbox Code Playgroud)

UINavigationController主类xib中的类设置为CustomNavigationController.希望它有助于ypu ..


joh*_*nyu 21

您的视图控制器永远不会旋转到应用程序本身不支持的任何位置.您应该启用所有可能的旋转,然后在不应旋转的视图控制器中放置以下行

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)

在ChatView中,它应该是:

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}
Run Code Online (Sandbox Code Playgroud)

如果您需要在轮换后更改布局,则应对子视图执行相应的更改

- (void)viewWillLayoutSubviews
Run Code Online (Sandbox Code Playgroud)

使用self.view.bounds检查的电流的大小view,因为self.view.frame旋转后也不会改变.

  • 我试过你的代码.但它没有帮助.视图仍然旋转到横向,我只想在肖像中 (2认同)

Ted*_*Ted 16

对于viewcontroller.m您想要旋转的特定

添加此方法:

- (BOOL)canAutoRotate
{
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

在你的内心 AppDelegate.m

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    UIViewController *currentViewController = [self topViewController];

    if ([currentViewController respondsToSelector:@selector(canAutoRotate)]) {
        NSMethodSignature *signature = [currentViewController methodSignatureForSelector:@selector(canAutoRotate)];

        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];

        [invocation setSelector:@selector(canAutoRotate)];
        [invocation setTarget:currentViewController];

        [invocation invoke];

        BOOL canAutorotate = NO;
        [invocation getReturnValue:&canAutorotate];

        if (canAutorotate) {
            return UIInterfaceOrientationMaskAll;
        }
    }

    return UIInterfaceOrientationMaskPortrait;
}

- (UIViewController *)topViewController
{
    return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}

- (UIViewController *)topViewControllerWithRootViewController:(UIViewController *)rootViewController
{
    if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController* tabBarController = (UITabBarController*)rootViewController;
        return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
    } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController* navigationController = (UINavigationController*)rootViewController;
        return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
    } else if (rootViewController.presentedViewController) {
        UIViewController* presentedViewController = rootViewController.presentedViewController;
        return [self topViewControllerWithRootViewController:presentedViewController];
    } else {
        return rootViewController;
    }
}
Run Code Online (Sandbox Code Playgroud)