Dav*_*wer 94 iphone cocoa-touch objective-c
我有一个应用程序,我想在某些视图中支持设备旋转,但其他在横向模式下没有特别有意义,所以当我交换视图时,我想强制旋转设置为纵向.
在UIDevice上有一个未记录的属性设置器可以完成这个技巧,但显然会产生编译器警告,并且可能会随着SDK的未来版本而消失.
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
Run Code Online (Sandbox Code Playgroud)
是否有任何记录的方法来强制定位?
更新:我想我会提供一个例子,因为我没有找到shouldAutorotateToInterfaceOrientation,因为我已经实现了它.
我希望我的应用程序支持View 1中的横向和纵向,但只支持View 2中的纵向.我已经为所有视图实现了shouldAutorotateToInterfaceOrientation但是如果用户在View 1中处于横向模式然后切换到View 2,我想强制执行手机旋转回肖像.
小智 101
事实很久之后,但是如果有人不使用导航控制器和/或不希望使用未记录的方法:
UIViewController *c = [[UIViewController alloc]init];
[self presentModalViewController:c animated:NO];
[self dismissModalViewControllerAnimated:NO];
[c release];
Run Code Online (Sandbox Code Playgroud)
提供和解除香草视图控制器就足够了.
显然,您仍然需要确认或拒绝覆盖shouldAutorotateToInterfaceOrientation的方向.但这会导致shouldAutorotate ......被系统再次调用.
Bde*_*eez 16
据我所知,该setOrientation:方法不起作用(或者可能不再起作用).这是我正在做的事情:
首先,将此定义放在文件的顶部,在#imports下面:
#define degreesToRadian(x) (M_PI * (x) / 180.0)
Run Code Online (Sandbox Code Playgroud)
然后,在viewWillAppear:方法中
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
}
Run Code Online (Sandbox Code Playgroud)
如果你想要动画,那么你可以将整个事物包装在动画块中,如下所示:
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
}
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)
然后,在纵向模式控制器中,您可以执行相反操作 - 检查其当前是否处于横向状态,如果是,则将其旋转回纵向.
Mic*_*ord 16
如果你想强制它从纵向旋转到横向,这里是代码.请注意,您需要调整视图的中心.我注意到我没有将视图放在正确的位置.否则,它完美地运作.谢谢你的提示.
if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation)){
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
self.view.bounds = CGRectMake(0.0f, 0.0f, 480.0f, 320.0f);
self.view.center = CGPointMake(160.0f, 240.0f);
[UIView commitAnimations];
}
Run Code Online (Sandbox Code Playgroud)
我有一个问题,我UIViewController在屏幕上UINavigationController,在横向方向上.但是,当在流程中按下下一个视图控制器时,我需要设备返回纵向.
我注意到的是,shouldAutorotateToInterfaceOrientation:当一个新的视图控制器被推入堆栈时不会调用该方法,但是当从堆栈中弹出一个视图控制器时会调用它.
利用这一点,我在我的一个应用程序中使用了这段代码:
- (void)selectHostingAtIndex:(int)hostingIndex {
self.transitioning = YES;
UIViewController *garbageController = [[[UIViewController alloc] init] autorelease];
[self.navigationController pushViewController:garbageController animated:NO];
[self.navigationController popViewControllerAnimated:NO];
BBHostingController *hostingController = [[BBHostingController alloc] init];
hostingController.hosting = [self.hostings objectAtIndex:hostingIndex];
[self.navigationController pushViewController:hostingController animated:YES];
[hostingController release];
self.transitioning = NO;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if (self.transitioning)
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
else
return YES;
}
Run Code Online (Sandbox Code Playgroud)
基本上,通过创建一个空视图控制器,将其推入堆栈,并立即将其弹出,可以使界面恢复到纵向位置.一旦控制器弹出,我只需按下我打算首先推动的控制器.在视觉上,它看起来很棒 - 用户从未看到空的任意视图控制器.
我一直在挖掘和挖掘寻找一个很好的解决方案.找到了这篇诀窍的博客文章:从密钥中删除最外层视图UIWindow并再次添加,系统将重新查询视图shouldAutorotateToInterfaceOrientation:控制器中的方法,强制执行正确的方向.看到它:iphone迫使uiview重新定位
有一种简单的方法可以以编程方式强制iPhone进入必要的方向 - 使用kdbdallas已经提供的两个答案,Josh:
//will rotate status bar
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
//will re-rotate view according to statusbar
UIViewController *c = [[UIViewController alloc]init];
[self presentModalViewController:c animated:NO];
[self dismissModalViewControllerAnimated:NO];
[c release];
Run Code Online (Sandbox Code Playgroud)
奇迹般有效 :)
编辑:
对于iOS 6我需要添加此功能:(适用于模态viewcontroller)
- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight);
}
Run Code Online (Sandbox Code Playgroud)
这在后来的iPhone 3.1.2 SDK上不再是问题.现在,它似乎遵守被推回到堆栈的视图的请求方向.这可能意味着您需要检测较旧的iPhone OS版本,并且仅在最新版本之前应用setOrientation.
目前尚不清楚Apple的静态分析是否会理解您正在解决较旧的SDK限制.我个人已经告诉我在我的下一次更新中删除方法调用,所以我还不确定是否有旧设备的黑客攻击会通过批准过程.
Josh的回答对我来说很好.
但是,我更喜欢发布"方向确实更改,请更新UI"通知.当视图控制器收到此通知时,它会调用shouldAutorotateToInterfaceOrientation:,允许您通过返回YES所需的方向来设置任何方向.
[[NSNotificationCenter defaultCenter] postNotificationName:UIDeviceOrientationDidChangeNotification object:nil];
Run Code Online (Sandbox Code Playgroud)
唯一的问题是,这会强制重新定位而不需要动画.您需要在此之间包含此行beginAnimations:并commitAnimations实现平滑过渡.
希望有所帮助.