Tec*_*ain 11 iphone objective-c rotation ipad ios
我有一个9-10屏幕的应用程序.我UINavigationController在我的视图控制器中嵌入了一个.我有几个视图控制器,我想只设置纵向:这意味着旋转设备不应该将这些视图控制器旋转到横向模式.我尝试了以下解决方案:
第一:
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
Run Code Online (Sandbox Code Playgroud)
但屏幕仍然旋转到风景.
第二:
我创建了一个自定义视图控制器类,PortraitViewController并在下面添加了代码PortraitViewController.m
@interface PortraitViewController ()
@end
@implementation PortraitViewController
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
//Here check class name and then return type of orientation
return UIInterfaceOrientationMaskPortrait;
}
@end
Run Code Online (Sandbox Code Playgroud)
之后我实现PortraitViewController.h了基类
#import <UIKit/UIKit.h>
#import "PortraitViewController.h"
@interface Login : PortraitViewController
@end
Run Code Online (Sandbox Code Playgroud)
它根本不起作用,仍允许视图控制器以横向模式旋转.
有没有其他解决方案我使用iOS 8并且不希望viewcontroller在横向模式下旋转?
编辑: 是否可以仅为某些视图控制器设置横向方向,并强制其他视图控制器方向粘贴到纵向?
尝试子类化UINavigationController您正在使用的因为默认情况下UINavigationController不将shouldAutorotate方法转发给您的viewcontroller.
在UINavigationController子类中实现以下方法
- (BOOL)shouldAutorotate
{
return [self.visibleViewController shouldAutorotate];
}
Run Code Online (Sandbox Code Playgroud)
现在UINavigationController将方法调用转发到其当前可见,UIViewController因此您需要shouldAutorotate单独实现它以获得所需的效果.
你的PortraitViewController还可以,我不知道你的Storyboard配置.在你的情况下,重要的是你应该将目标视图控制器嵌入到另一个新的导航控制器中,然后将它的类设置为您的PortraitViewController并以模态方式呈现该导航,就像我在下面的图像中所做的那样并且它按预期工作.
下面是我的UINavigationController的PortrateNavigation子类
PortrateNavigation.h
#import <UIKit/UIKit.h>
@interface PortrateNavigation : UINavigationController
@end
Run Code Online (Sandbox Code Playgroud)
PortrateNavigation.m
#import "PortrateNavigation.h"
@implementation PortrateNavigation
- (void)viewDidLoad {
[super viewDidLoad];
}
- (BOOL)shouldAutorotate {
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
@end
Run Code Online (Sandbox Code Playgroud)
试着沿增加这种方法shouldAutorotate和supportedInterfaceOrientations
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
//Setting the orientation of the view. I've set it to portrait here.
return UIInterfaceOrientationPortrait;
Run Code Online (Sandbox Code Playgroud)
}
您也可以使用它[UIViewController attemptRotationToDeviceOrientation],以便它旋转到所需的新方向
你必须手动完成。所有视图控制器,在那些您不想旋转视图的视图控制器中,实现以下方法。
- (BOOL)shouldAutorotate
{
return NO;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12490 次 |
| 最近记录: |