Iphone UITabBar更改“旋转”上的所有视图
我有一个具有多个视图的选项卡栏控制器。每个视图都有一个工具栏。我添加了将背景图像应用于工具栏的代码 我还在viewdidLoad中的每个视图上添加了代码,以便在设备旋转时触发,以便我可以为横向模式应用不同的背景图像:Iphone UITabBar更改“旋转”上的所有视图,iphone,uitabbarcontroller,Iphone,Uitabbarcontroller,我有一个具有多个视图的选项卡栏控制器。每个视图都有一个工具栏。我添加了将背景图像应用于工具栏的代码 我还在viewdidLoad中的每个视图上添加了代码,以便在设备旋转时触发,以便我可以为横向模式应用不同的背景图像: [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; NSNotificationCenter *notificationCenter = [NSNotificationCenter d
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:@selector(didRotate:)
name:@"UIDeviceOrientationDidChangeNotification" object:nil];
如果我运行应用程序并旋转,第一个视图会工作,但是转到其他选项卡会导致didRotate方法无法启动,因为设备已经旋转
如何在设备旋转时更新所有视图?您应该做的是检查
视图上的界面方向将出现:
并根据需要重新布局UI。您不一定需要通知。您可以使用这两种方法来实现这一点
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation
duration:(NSTimeInterval)duration
及
例如,要使选项卡栏在横向模式下隐藏,而不是在纵向模式下隐藏,请使用以下命令:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation
duration:(NSTimeInterval)duration
{
if (toOrientation == UIInterfaceOrientationLandscapeLeft ||
toOrientation == UIInterfaceOrientationLandscapeRight) {
[[self view] endEditing:YES];
[[self view] addSubview:graphView];
//[[UIApplication sharedApplication] setStatusBarHidden:TRUE withAnimation:FALSE];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
[self.navigationController setNavigationBarHidden:TRUE animated:TRUE];
}
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
UIInterfaceOrientation toOrientation = self.interfaceOrientation;
if ( self.tabBarController.view.subviews.count >= 2 )
{
UIView *transView = [self.tabBarController.view.subviews objectAtIndex:0];
UIView *tabBar = [self.tabBarController.view.subviews objectAtIndex:1];
if(toOrientation == UIInterfaceOrientationLandscapeLeft ||
toOrientation == UIInterfaceOrientationLandscapeRight) {
transView.frame = CGRectMake(0, 0, 480, 320 );
tabBar.hidden = TRUE;
}
else
{
transView.frame = CGRectMake(0, 0, 320, 480);
tabBar.hidden = FALSE;
}
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation
duration:(NSTimeInterval)duration
{
if (toOrientation == UIInterfaceOrientationLandscapeLeft ||
toOrientation == UIInterfaceOrientationLandscapeRight) {
[[self view] endEditing:YES];
[[self view] addSubview:graphView];
//[[UIApplication sharedApplication] setStatusBarHidden:TRUE withAnimation:FALSE];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
[self.navigationController setNavigationBarHidden:TRUE animated:TRUE];
}
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
UIInterfaceOrientation toOrientation = self.interfaceOrientation;
if ( self.tabBarController.view.subviews.count >= 2 )
{
UIView *transView = [self.tabBarController.view.subviews objectAtIndex:0];
UIView *tabBar = [self.tabBarController.view.subviews objectAtIndex:1];
if(toOrientation == UIInterfaceOrientationLandscapeLeft ||
toOrientation == UIInterfaceOrientationLandscapeRight) {
transView.frame = CGRectMake(0, 0, 480, 320 );
tabBar.hidden = TRUE;
}
else
{
transView.frame = CGRectMake(0, 0, 320, 480);
tabBar.hidden = FALSE;
}
}
}