我有一个应用程序,我想在某些视图中支持设备旋转,但其他在横向模式下没有特别有意义,所以当我交换视图时,我想强制旋转设置为纵向.
在UIDevice上有一个未记录的属性设置器可以完成这个技巧,但显然会产生编译器警告,并且可能会随着SDK的未来版本而消失.
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
Run Code Online (Sandbox Code Playgroud)
是否有任何记录的方法来强制定位?
更新:我想我会提供一个例子,因为我没有找到shouldAutorotateToInterfaceOrientation,因为我已经实现了它.
我希望我的应用程序支持View 1中的横向和纵向,但只支持View 2中的纵向.我已经为所有视图实现了shouldAutorotateToInterfaceOrientation但是如果用户在View 1中处于横向模式然后切换到View 2,我想强制执行手机旋转回肖像.
我正在制作一个摄影应用程序,允许以肖像或风景拍摄照片.由于项目的要求,我不能让设备方向自动旋转,但确实需要支持旋转.
使用以下定位方法时:
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if self.orientation == .Landscape {
return UIInterfaceOrientationMask.LandscapeRight
} else {
return UIInterfaceOrientationMask.Portrait
}
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
if self.orientation == .Landscape {
return UIInterfaceOrientation.LandscapeRight
} else {
return UIInterfaceOrientation.Portrait
}
}
Run Code Online (Sandbox Code Playgroud)
我可以在启动时正确设置旋转.通过更改方向值并调用UIViewController.attemptRotationToDeviceOrientation()我能够支持旋转到新的所需界面.但是,此旋转仅在用户实际移动其设备时发生.我需要它自动发生.
我可以调用:UIDevice.currentDevice().setValue(targetOrientation.rawValue, forKey: "orientation")强制更改,但这会导致其他副作用,因为UIDevice.currentDevice().orientation只返回该点的setValue.(而且非常脏)
有什么我想念的吗?我已经研究过关闭并启动一个新的视图控制器,但这有其他问题,例如在解除时立即出现UI故障并立即呈现新的视图控制器.
编辑:
以下方法对我不起作用:
编辑2:
对潜在解决方案的思考
直接设置方向(带setValue)并处理iOS 9上出现的所有副作用(不可接受)
我可以使用当前的解决方案并指示用户需要旋转设备.设备物理旋转后,UI会旋转,然后正确锁定到位.(差的UI)
我可以找到一种解决方案,强制刷新方向并在没有物理操作的情况下旋转.(我在问什么,并寻找)
手工完成所有工作.我可以纵向或横向锁定界面,并手动旋转和调整容器视图的大小.这是"脏"的,因为它放弃了所有大小类自动布局功能,并导致更重的代码.我试图避免这种情况.
我有一个TabBar基础应用程序,它仅支持一个特殊视图(UINaviagtionController的根视图)的Landscape方向.现在我想为此导航控制器强制所有其他视图的纵向方向.我试过用
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];
这很好用,但这段代码是私人api调用,我不能冒着拒绝app的风险.
我还试图手动旋转下一个视图但这只旋转视图而不是导航或标签栏.
是否有类似的方法来强制改变方向?
有人知道怎么做吗?
我想这个:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
}
- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
}
- (void)didAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
}
- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration {
}
Run Code Online (Sandbox Code Playgroud)
可能会阻止设备旋转(覆盖我的UIViewController的所有旋转方法而不是调用超类)但我担心它不是实际执行旋转的UIViewController.
我的UIViewController在UINavigationController中.
有人有主意吗?
干杯,尼克.
更新2
在我的UITabBarController子类中,我尝试添加这个:
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
Run Code Online (Sandbox Code Playgroud)
现在,每次我选择一个标签项时,设备都会完美地旋转到纵向模式.但是,现在我可以在选择(a)时旋转设备,设备将旋转到横向模式.如何阻止设备旋转?
我相信我的tab控制器子类中的这个方法是导致这种情况的原因:
- (BOOL)shouldAutorotate {
if(self.selectedIndex == 0)
{
return NO;
}
return [self.viewControllers.lastObject shouldAutorotate];
}
Run Code Online (Sandbox Code Playgroud)
如果我返回"否",当我在视图控制器中时,我无法旋转,但是当我选择它时,它不会自动旋转为纵向.如果我返回"是",我可以在视图控制器中旋转,但是当我选择它时,它会自动旋转为纵向.
我的应用程序中有一个自定义标签栏控制器,具有以下层次结构:
UITabBarController
|
UINavigationController
| |
| UIViewController(a)
|
UINavigationController
| |
| UIViewController(b)
|
UINavigationController
|
UIViewController(c)
Run Code Online (Sandbox Code Playgroud)
我希望视图控制器(a)只能在纵向模式下查看,而视图控制器(b)和(c)可以在所有方向上查看,但是可以上下颠倒.现在,我可以单独为每个视图控制器执行此操作,但是当我处于横向模式(b)并选择(a)的选项卡项时,我的问题就出现了,然后以横向模式显示a,这看起来不像好.如何确保标签栏(或视图控制器)检查是否可以在当前方向查看要选择的视图控制器?
如果需要,这是我的代码(a)将其自身限制为纵向模式:
- (BOOL) shouldAutorotate
{
return NO;
}
- (NSUInteger) supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)
UPDATE
我已将其添加到视图控制器(a)中,但我在视图中间得到一个黑色方块,导航栏中的标题不再居中.
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait]; …Run Code Online (Sandbox Code Playgroud) objective-c rotation uitabbarcontroller uiviewcontroller ios
我使用以下代码来设置设备方向
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
Run Code Online (Sandbox Code Playgroud)
当我使用我得到警告,我发现以下代码来解决该警告.
@interface UIDevice (MyPrivateNameThatAppleWouldNeverUseGoesHere)
- (void) setOrientation:(UIInterfaceOrientation)orientation;
@end
Run Code Online (Sandbox Code Playgroud)
现在我想知道的是......
应用程序商店是否可以接受此代码在应用程序中?
谢谢你的帮助!.