1 旋转 - transform
UIView有一个transform属性, 设置它可以让这个view进行旋转. 但是, 如果想把它作为横竖屏切换来使用, 会有问题:弹出的Alert或者键盘等等, 都是原来的方向.
2 旋转 - setOrientation
UIDevice有一个setOrientation方法, 不过现在被苹果公司隐藏起来了. 我们需要通过间接的方式来调用它, 从而使程序横屏. 具体看后面的样例代码.
3 旋转 - presentViewController
我们通过presentViewController来呈现一个新视图, 在这个新视图中重新设置方向, 从而产生横屏的视图.
4 其他
通过2,3的方法, 让程序横屏, 还需要配合几点:
01.在TARGETS-General中, 所需要的方向必须有配置
02.将要旋转的视图, 必须重写两个方法:
- (BOOL)shouldAutorotate;
- (UIInterfaceOrientationMask)supportedInterfaceOrientations;
5 样例代码
// UIRootViewController.m
#import "UIRootViewController.h"
#import "UINextViewController.h"
@interface UIRootViewController ()
@property (nonatomic, assign) BOOL bLandscapeRight;
@end
@implementation UIRootViewController
- (void)loadView
{
[super loadView];
self.title = @"x";
UIButton *showButton1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
showButton1.frame = CGRectMake(80, 80, 80, 30);
showButton1.layer.borderWidth = 1;
[showButton1 setTitle:@"旋转本视图" forState:UIControlStateNormal];
[showButton1 addTarget:self action:@selector(showButton1Pressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:showButton1];
UIButton *showButton2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
showButton2.frame = CGRectMake(80, 120, 80, 30);
showButton2.layer.borderWidth = 1;
[showButton2 setTitle:@"旋转新视图" forState:UIControlStateNormal];
[showButton2 addTarget:self action:@selector(showButton2Pressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:showButton2];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(80, 160, 80, 30)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.text = self.title;
[self.view addSubview:textField];
}
#pragma mark - 通过setOrientation消息旋转屏幕
- (void)showButton1Pressed
{
if (self.bLandscapeRight)
{
self.bLandscapeRight = NO;
[self forceOrientation:UIInterfaceOrientationPortrait];
}
else
{
self.bLandscapeRight = YES;
[self forceOrientation:UIInterfaceOrientationLandscapeRight];
}
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return self.bLandscapeRight ? UIInterfaceOrientationMaskLandscape : UIInterfaceOrientationMaskPortrait;
}
- (void)forceOrientation: (UIInterfaceOrientation)orientation
{
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
{
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = orientation;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
#pragma mark - 通过设置新的presentViewController旋转屏幕
- (void)showButton2Pressed
{
UINextViewController *nextViewController = [[UINextViewController alloc] initWithTitle:0];
[self presentViewController:nextViewController animated:YES completion:nil];
}
@end
// UINextViewController.m
#import "UINextViewController.h"
@implementation UINextViewController
- (id)initWithTitle:(int)num
{
self = [super init];
if (self)
{
self.title = [NSString stringWithFormat:@"%d", num];
}
return self;
}
- (void)loadView
{
[super loadView];
self.view.backgroundColor = [UIColor orangeColor];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, 180, 50)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.text = self.title;
[self.view addSubview:textField];
UIButton *showButton = [[UIButton alloc] initWithFrame:CGRectMake(210, 20, 50, 50)];
showButton.layer.borderWidth = 1;
[showButton setTitle:@"弹框" forState:UIControlStateNormal];
[showButton addTarget:self action:@selector(showButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:showButton];
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(270, 20, 50, 50)];
backButton.layer.borderWidth = 1;
[backButton setTitle:@"返回" forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backButton];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(@"rootViewController.presentingViewController: title = %@, class = %@", self.view.window.rootViewController.presentingViewController.title, self.view.window.rootViewController.presentingViewController.class);
NSLog(@" rootViewController.presentedViewController: title = %@, class = %@", self.view.window.rootViewController.presentedViewController.title, self.view.window.rootViewController.presentedViewController.class);
NSLog(@" self.presentingViewController: title = %@, class = %@", self.presentingViewController.title, self.presentingViewController.class);
NSLog(@" self.presentedViewController: title = %@, class = %@", self.presentedViewController.title, self.presentedViewController.class);
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (void)backButtonPressed
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)showButtonPressed
{
UINextViewController *landscapeView = [[UINextViewController alloc] initWithTitle:self.title.intValue + 1];
[self presentViewController:landscapeView animated:YES completion:nil];
}
@end