UITabBar 更改旋转时的所有视图

发布于 2024-11-09 17:58:46 字数 532 浏览 0 评论 0原文

我有一个具有多个视图的选项卡栏控制器。每个视图都有一个工具栏。我添加了代码以将背景图像应用到工具栏。

我还在 viewdidLoad 中添加了代码,在设备旋转时在每个视图上触发,以便我可以为横向模式应用不同的背景图像:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:@selector(didRotate:)
name:@"UIDeviceOrientationDidChangeNotification" object:nil];

如果我运行应用程序并旋转,第一个视图可以工作,但随后转到其他选项卡会导致didRotate 方法不会触发,因为设备已经旋转。

如何让设备旋转时所有视图都更新?

I have a tab bar controller with multiple views. Each of the views has a toolbar. I added code to apply a background image to the toolbar.

I also added code, in viewdidLoad, on each view to fire when the device is rotated so I can apply a different background image for landscape mode:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:@selector(didRotate:)
name:@"UIDeviceOrientationDidChangeNotification" object:nil];

If I run the app and rotate, the first view works, but then going to other tabs causes the didRotate method to not fire, since the device has already rotated.

How can I make all the views update when the device rotates?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不交电费瞎发啥光 2024-11-16 17:58:46

您应该做的是检查 viewWillAppear: 上的 interfaceOrientation 并根据需要重新布局 UI。

What you should do is to check the interfaceOrientation on viewWillAppear: and re-layout the UI if needed.

一场信仰旅途 2024-11-16 17:58:46

您不一定需要通知。您可以使用两种方法来完成此操作

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation 
                                duration:(NSTimeInterval)duration

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 

例如,要使 tabBar 在横向模式下隐藏但不在纵向模式下隐藏,请使用以下命令:

- (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;
        }
    }
}

You do not necessarily need notifications. You can accomplish this using the two methods

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation 
                                duration:(NSTimeInterval)duration

and

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 

For example, to make the tabBar hide in landscape mode but not in portrait, use this:

- (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;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文