Joh*_*art 71 xcode autorotate ios autolayout ios7
我正在使用AutoLayout在iPad应用程序上工作,如果用户启用某种模式("抬头"模式),我想只支持纵向(或纵向颠倒)方向,此外,如果设备在风景,我想自动切换到纵向模式.
在顶视图控制器中,我有以下内容:
- (NSUInteger) supportedInterfaceOrientations {
if (self.modeHeadsUp) {
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
} else {
return UIInterfaceOrientationMaskAll;
}
}
- (BOOL) shouldAutorotate {
return TRUE;
}
Run Code Online (Sandbox Code Playgroud)
基于我在其他地方看到的答案,答案似乎是我应该使用"application setStatusBarOrientation".因此,在用户选择"抬头"模式的方法中,我包括:
UIApplication *application = [UIApplication sharedApplication];
[application setStatusBarOrientation:UIInterfaceOrientationPortrait
animated:YES];
Run Code Online (Sandbox Code Playgroud)
但是,这根本就没有做任何事情.虽然我可以物理移动设备以使其旋转为纵向,但它不会自动移动.
实际上,在运行上面的代码后尝试以横向模式尝试以编程方式设置方向时,当我使用以下代码查询应用程序"statusBarOrientation"时,它对于横向保持为"4":
UIApplication *application = [UIApplication sharedApplication];
int orientation = [application statusBarOrientation];
self.movesTextView.text = [NSString stringWithFormat:@"ORIENTATION %d", orientation];
Run Code Online (Sandbox Code Playgroud)
好像autolayout可能没有被setStatusBarOrientation触发,所以我试图在之后添加这个代码,没有效果:
[super updateViewConstraints];
[self.view updateConstraints];
Run Code Online (Sandbox Code Playgroud)
我意识到Apple希望将设备方向放在用户手中.但是,我希望能够在不进行"抬头"模式时支持横向模式.
我错过了能够强制改变方向的东西吗?
Sun*_*hah 190
对于iOS 7和8:
Objective-C的:
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
Run Code Online (Sandbox Code Playgroud)
Swift 3+:
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
Run Code Online (Sandbox Code Playgroud)
我打电话给它- viewDidAppear:.
Arp*_*xit 20
用这个.定位问题的完美解决方案..更早和更早
[[UIDevice currentDevice] setValue:
[NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
forKey:@"orientation"];
Run Code Online (Sandbox Code Playgroud)
sna*_*nak 10
当条件发生变化时,您需要调用attemptRotationToDeviceOrientation (UIViewController)系统调用supportedInterfaceOrientations.
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft]; [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
Run Code Online (Sandbox Code Playgroud)
确实可以,但是您必须在视图控制器中返回shouldAutorotate并使用YES
- (BOOL)shouldAutorotate
{
return self.shouldAutoRotate;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果这样做,如果用户旋转设备,VC将自动旋转...因此我将其更改为:
@property (nonatomic, assign) BOOL shouldAutoRotate;
- (BOOL)shouldAutorotate
{
return self.shouldAutoRotate;
}
Run Code Online (Sandbox Code Playgroud)
我打电话
- (void)swithInterfaceOrientation:(UIInterfaceOrientation)orientation
{
self.rootVC.shouldAutoRotate = YES;
NSNumber *value = [NSNumber numberWithInt: orientation];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
Run Code Online (Sandbox Code Playgroud)
单击按钮以强制新的方向。要将shouldAutoRotate设置为NO,我将其添加到了rootVC
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
self.shouldAutoRotate = NO;
}
Run Code Online (Sandbox Code Playgroud)
PS:此替代方法也适用于所有模拟器。
| 归档时间: |
|
| 查看次数: |
103420 次 |
| 最近记录: |