我的主视图控制器为UITabBarController
,标签栏项为4 UINavigationControllers
。每个导航栏都有几个不同的视图,可以将其推到堆栈上。
我有一个我想要自动旋转的视图,但我无法在该视图控制器中调用willAnimateFirstHalfOfRotationToInterfaceOrientation
。
我已尝试对UITabBarController
和UINaviationControllers
进行子类化,并为shouldAutorotateToInterfaceOrientation
添加覆盖,如下所示:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
即便如此,我无法让视图旋转。我想也许最顶级的UIViewController
(包括标签和导航)必须能够旋转。甚至每个视图都在链上。我已经尝试为每个控制器覆盖shouldAutorotateToInterfaceOrientation
但仍无法使视图旋转。
有人完成了这项工作或有任何建议吗?
提前致谢!
答案 0 :(得分:1)
除了像覆盖houldAutorotateToInterfaceOrientation一样对tabBarController进行子类化之外,还必须执行以下操作:
在控制器的viewDidLoad方法中,要添加要旋转的视图:
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
在要添加要旋转的视图的控制器中添加此委托方法:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
//NSLog(@"didRotateFromInterfaceOrientation");
if((fromInterfaceOrientation == UIInterfaceOrientationPortrait) ||
(fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
{
YourAppDelegate *mainDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
[mainDelegate.tabBarController.view addSubview: viewToBeRotated];
[viewToBeRotated setHidden:NO];
return;
}
if(fromInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
[viewToBeRotated removeFromSuperview];
[self.view setHidden:NO];
}
}
您可能还想添加以下方法:
- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
{
//self.view = self.portrait;
YourAppDelegate *mainDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
[viewToBeRotated removeFromSuperview];
mainDelegate.tabBarController.view.transform = CGAffineTransformIdentity;
mainDelegate.tabBarController.view.transform = CGAffineTransformMakeRotation(degreesToRadian(0));
mainDelegate.tabBarController.view.bounds = CGRectMake(0.0, 0.0, 300.0, 480.0);
}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
{
YourAppDelegate *mainDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
[mainDelegate.tabBarController.view addSubview: viewToBeRotated];
mainDelegate.tabBarController.view.transform = CGAffineTransformIdentity;
mainDelegate.tabBarController.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90));
mainDelegate.tabBarController.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0);
}
else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
//self.view = self.portrait;
YourAppDelegate *mainDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
[viewToBeRotated removeFromSuperview];
mainDelegate.tabBarController.view.transform = CGAffineTransformIdentity;
mainDelegate.tabBarController.view.transform = CGAffineTransformMakeRotation(degreesToRadian(180));
mainDelegate.tabBarController.view.bounds = CGRectMake(0.0, 0.0, 300.0, 480.0);
}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
YourAppDelegate *mainDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
[mainDelegate.tabBarController.view addSubview: viewToBeRotated];
mainDelegate.tabBarController.view.transform = CGAffineTransformIdentity;
mainDelegate.tabBarController.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
mainDelegate.tabBarController.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0);
}
}
您可能还想看看
Rotating the iPhone, and instantiating a new UIViewController
TabBarController and navigationControllers in landscape mode
答案 1 :(得分:1)
您可以使用SubClassed UITabBarController
(或使用附加内容)询问所选的navigationController,以获取visibleViewController
对轮换请求的响应:
@implementation RotatingTabBarController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if([self.selectedViewController isKindOfClass:[UINavigationController class]]){
return [[(UINavigationController*)self.selectedViewController visibleViewController] shouldAutorotateToInterfaceOrientation:interfaceOrientation];
} else {
return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
}
@end
答案 2 :(得分:0)
继承UITabBarController并重写此方法:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
我认为我的真正问题是XCode没有编译更新的代码。我做了Build clean,触摸并重新启动了XCode和模拟器,最后更改了。