Move UITabBar to the top of a UITabBarController?

2019-02-11 05:47发布

I just finished designing a application that uses a top bar that toggles between three UIViewControllers. My first thought was to use a UITabBarController, since it works very simular.

However, the tabBar is at the bottom and in my PSD, it's at the top. Is there anyway I can change it? I see in the library I can drag in a UITabBar, but I don't know how to get it to change pages from there.

Please help! Coulton

2条回答
Summer. ? 凉城
2楼-- · 2019-02-11 05:56

Hope the below code helps

UITabBarController *tabC = [[UITabBarController alloc]init];
    tabC.tabBar.frame = CGRectMake(0, 0, 320, 70);

    NSArray *arr = [[NSArray alloc]initWithObjects:firstObj,secObj, nil];

    tabC.viewControllers = arr;

    [self.window addSubview:tabC.view];

Worked fine for me

查看更多
甜甜的少女心
3楼-- · 2019-02-11 06:14

I think it can be done, but you should be aware of this:

"Important: In iPhone OS 3.0 and later, you should not attempt to use the methods and properties of this class to modify the tab bar when it is associated with a tab bar controller object. Modifying the tab bar in this way results in the throwing of an exception. Instead, any modifications to the tab bar or its items should occur through the tab bar controller interface. You may still directly modify a tab bar object that is not associated with a tab bar controller."

So, in your UI (I suppose you defined it in Interface Builder), instantiate a UITabBar object (by choosing it in the IB library and dragging it to your view in IB). Choose the default position and also define the way that this tab bar autoresizes (using the size pane of the info window). Then, add an outlet to your view controller of type UITabBar, and, finally, connect the tab bar object to the tab bar outlet.

Once you have done this, in your view controller's viewDidLoad method you can customize any property of the tab bar that you want to.

查看更多
登录 后发表回答