iOS - 横向模式下的 UIView 不旋转
Posted
技术标签:
【中文标题】iOS - 横向模式下的 UIView 不旋转【英文标题】:iOS - UIView in landscape mode is not rotating 【发布时间】:2013-03-14 07:39:39 【问题描述】:我正在为 iPhone 和 iPad 编写一个应用程序支持。 iPhone 需要纵向模式,iPad 需要使用横向。
在 AppDelegate.m
if ([CKAppDelegate isPad])
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
CGSize size = [self getScreenSize];
navigationController = [[UINavigationController alloc] initWithRootViewController:[[PadMainViewController alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)]];
else
CGSize size = [self getScreenSize];
navigationController = [[UINavigationController alloc] initWithRootViewController:[[MainViewController alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)]];
self.window.rootViewController = navigationController;
...
- (CGSize)getScreenSize
CGSize size;
if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) && [[UIScreen mainScreen] bounds].size.height > [[UIScreen mainScreen] bounds].size.width)
// in Landscape mode, width always higher than height
size.width = [[UIScreen mainScreen] bounds].size.height;
size.height = [[UIScreen mainScreen] bounds].size.width;
else if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) && [[UIScreen mainScreen] bounds].size.height < [[UIScreen mainScreen] bounds].size.width)
// in Portrait mode, height always higher than width
size.width = [[UIScreen mainScreen] bounds].size.height;
size.height = [[UIScreen mainScreen] bounds].size.width;
else
// otherwise it is normal
size.height = [[UIScreen mainScreen] bounds].size.height;
size.width = [[UIScreen mainScreen] bounds].size.width;
return size;
在 PadMainViewController.m
- (id)initWithFrame:(CGRect)frame
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width * 0.7, frame.size.height)];
leftView.backgroundColor = [UIColor cyanColor];
UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width * 0.3, frame.size.height)];
rightView.backgroundColor = [UIColor yellowColor];
self.chopInkButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.chopInkButton setTitle:@"Tap to ChopInk" forState:UIControlStateNormal];
self.chopInkButton.frame = CGRectMake(50, 200, 250, 44);
[self.chopInkButton addTarget:self action:@selector(chopInkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[rightView addSubview:self.chopInkButton];
self.logoImageView.frame = CGRectMake(50, 50, 100, 100);
[self.logoImageView.layer setBorderColor:[[UIColor blackColor] CGColor]];
[self.logoImageView.layer setBorderWidth:2.0f];
[rightView addSubview:self.logoImageView];
[self.view addSubview:leftView];
[self.view addSubview:rightView];
return self;
...
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation
return (UIInterfaceOrientationIsLandscape(toInterfaceOrientation));
我得到的结果是
在项目总结中,我已经禁用了 iPad 的纵向模式
知道为什么按钮不旋转吗?
【问题讨论】:
【参考方案1】:尝试使用以下方法:ios 6
- (NSUInteger)supportedInterfaceOrientations
return UIInterfaceOrientationMaskAll;
- (BOOL)shouldAutorotate
return YES;
【讨论】:
仍然无法正常工作。顺便说一句,我将部署目标设置为 5.0【参考方案2】:我通过改变来解决这个问题
PadMainViewController.m
- (id)initWithFrame:(CGRect)frame
...
return self;
到
- (id)initWithFrame:(CGRect)frame
if ((self = [super init]))
...
return self;
顺便说一句,initWithFrame
是自定义函数。
【讨论】:
以上是关于iOS - 横向模式下的 UIView 不旋转的主要内容,如果未能解决你的问题,请参考以下文章