iPhone 的 UITabBar 作为应用程序窗口的一部分;旋转设备时出现问题

发布于 2024-10-20 20:03:38 字数 1690 浏览 0 评论 0原文

我有一个 iPhone 应用程序,它使用导航控制器来显示顶部栏(带有标题和后退按钮等......)。

我向应用程序窗口添加了一个 UITabBar ,可以在其各个部分之间进行切换。我没有将选项卡栏添加到 ViewController 的每个视图中,而是将其添加到应用程序窗口中。

(当我将它放在 ViewController 中时,当动画弹出/推送发生时,控制器之间的切换使选项卡栏与整个视图一起向左/向右滑动)。

因此,我将 UITabBar 添加到 MainWindow.xib 中,并将其绑定到应用程序委托的变量。在我的 didFinishLaunchingWithOptions 方法中,我添加了以下代码:

[self.window addSubview:navigationController.view];

CGRect frame = navigationController.view.frame;
frame.size.height -= tabbar.frame.size.height;
navigationController.view.frame = frame;
tabbar.selectedItem = [tabbar.items objectAtIndex:0];

调整主(navigationController's)视图的大小,以使 TabBar 可见。

当我旋转设备时,问题就出现了——我的视图被拉伸到整个窗口,并且我失去了显示选项卡栏的能力。

我向 ViewController 添加了一个 - (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation) fromInterfaceOrientation 方法,代码如下:

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    CGRect frame = self.view.frame;
    frame.size.height -= [AppState shared].tabBar.frame.size.height;
    //frame.origin.y = [AppState shared].tabBar.frame.size.height;
    //frame.origin.x = 100;
    self.view.frame = frame;    
    frame = [AppState shared].tabBar.frame;
    frame.origin.y = [UIScreen mainScreen].bounds.size.height - frame.origin.y - frame.size.height;
    [AppState shared].tabBar.frame = frame;
}

它调整视图大小,并将选项卡栏移动到视图的上/下部分(我此处仅允许纵向/纵向颠倒方向)。问题是,我的 TabBar 也被颠倒了,而且它不再可单击。

如下图所示:

标签栏颠倒

有人知道如何处理这种情况吗?或者,如何使标签栏不依赖于视图控制器,但也能够平滑地处理界面的旋转?

I have an iPhone application that's using Navigation Controller to display the top bar (with title and back button, and such...).

I added a UITabBar to the application window, that enables to switch between the parts of it. Instead of adding the tab bar to each of ViewController's view I added the bar to app window.

(When I had it in the ViewController, switching between controllers made the tab bar to swipe left/right, when animated pop/push occured, together with whole view).

So, I added the UITabBar to the MainWindow.xib, and tied it to the app delegate's variable. In my didFinishLaunchingWithOptions method, I added the following code:

[self.window addSubview:navigationController.view];

CGRect frame = navigationController.view.frame;
frame.size.height -= tabbar.frame.size.height;
navigationController.view.frame = frame;
tabbar.selectedItem = [tabbar.items objectAtIndex:0];

to resize the main (navigationController's) view, in order to make the TabBar visible.

The problem shows up when I rotate the device -- my view gets stretched to full window and I loose the ability to show the TabBar.

I added a - (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation) fromInterfaceOrientation method to my ViewController, with the following code:

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    CGRect frame = self.view.frame;
    frame.size.height -= [AppState shared].tabBar.frame.size.height;
    //frame.origin.y = [AppState shared].tabBar.frame.size.height;
    //frame.origin.x = 100;
    self.view.frame = frame;    
    frame = [AppState shared].tabBar.frame;
    frame.origin.y = [UIScreen mainScreen].bounds.size.height - frame.origin.y - frame.size.height;
    [AppState shared].tabBar.frame = frame;
}

It resizes the view, and moves the tab bar to up/down part of the view (I allow only Portrait/Portrait upside down orientations here). The problem is, my TabBar is turned upside down as well, and also, it's no longer clickable.

It looks like the image below:

Tab bar upside down

Anyone knows how to handle this kind of situation? Or, how to make the tab bar not tied to view controller, but also able to handle interface's rotation smoothly?

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

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

发布评论

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

评论(1

三生路 2024-10-27 20:03:38

您正在以意想不到的方式使用选项卡栏。您似乎正在使用 UITabBarView 作为其他视图的不受控制的元素。那不是它的功能。

UITabBarView 应该由 UITabBarController 直接控制,而 UITabBarController 又应该控制选项卡栏中显示的视图的所有视图控制器,即选项卡栏控制器是一种控制子控制器的导航控制器。

假设您有三个选项卡,第三个选项卡是导航控制器。您的控制器层次结构将如下所示:

TabbarController:
    -->tab1ViewController
    -->tab2ViewController
    -->tab3ViewController(UINavigationController):
            -->rootViewController-->secondViewController

您正在尝试移动和管理标签栏视图,而无需其控制器和适当的控制器层次结构。那是行不通的。

You are using the tabbar in an unintended way. You seem to be using the UITabBarView as an uncontrolled element of other views. That is not it's function.

The UITabBarView should be controlled directly by a UITabBarController which in turn should be controlling all the view controllers for the views displayed in the tabbar i.e. the tabbar controller is a type of navigation controller that controls subcontrollers.

Suppose you have three tabs and the third one is a navigation controller. Your controller hierarchy would look like this:

TabbarController:
    -->tab1ViewController
    -->tab2ViewController
    -->tab3ViewController(UINavigationController):
            -->rootViewController-->secondViewController

You are trying to move and manage the tabbar view without its controller and the proper controller hierarchy. That isn't going to work.

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