UITabBarController和UINavigationController中的自动旋转视图
我的主视图控制器是UITabBarController
,其中4个UINavigationControllers
作为标签栏项目。每个导航栏都是几个不同的视图,它们被压入堆栈。UITabBarController和UINavigationController中的自动旋转视图
我有一个单一的视图,我想自动旋转,但我不能让willAnimateFirstHalfOfRotationToInterfaceOrientation
在视图控制器内被调用。
我试着子类我UITabBarController
和UINaviationControllers
并添加覆盖了shouldAutorotateToInterfaceOrientation
像这样:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
即使在那之后我不能得到的视图旋转。我想也许最上面的UIViewController
(包括标签和导航)必须能够旋转。甚至是连锁店的每一个观点。我试过为每个控制器重写shouldAutorotateToInterfaceOrientation
,但仍然无法旋转视图。
任何人都完成了这个或有任何建议吗?
在此先感谢!
除了继承你的tabBarController因为你是压倒一切的houldAutorotateToInterfaceOrientation,你必须做到以下几点:
在其中要添加一个视图控制器的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
子类的UITabBarController和重写此方法曾工作:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
我认为我真正的问题是XCode没有编译更新的代码。我做了建立清洁,触摸并重新启动XCode和模拟器,并最终采取了变化。
这让所有的视图控制器都可以旋转,我想。 – FelixLam 2009-11-17 01:03:45
您可以使用一个子类的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
oops,现在只看到这是一个老问题 – FelixLam 2009-11-17 01:09:08
实际上,我换出两种观点。我尝试设置autoResizingMask,但它仍然不起作用。您建议的其他两种方法应该由my - willAnimateFirstHalfOfRotationToInterfaceOrientation方法处理。它根据方向交换self.view。这个方法甚至都不会被调用。 – Rob 2009-06-15 23:23:42