开发者

iPhone's UITabBar as part of application window; problems when rotating device

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:

iPhone's UITabBar as part of application window; problems when rotating device

Anyone kno开发者_C百科ws 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜