How can I create my own UITabBar? [closed]

空扰寡人 提交于 2019-11-29 07:41:01

I don't know if there is a best way to do this, but I will share my experience:

I created my own version of a tab bar by subclassing UIView as opposed to UITabBar. As the linked question mentions, Apple's UI classes are quite finicky about their sizing and I do not think I could get the UITabBar subclass to size as I wanted.

Because I did not have a subclass of UITabBar I also ended up rolling my own versions of UITabBarController and UITabBarDelegate, with interfaces that are essentially the same as the Apple classes. I WAS able to use UITabBarItem to store the title and icon for the buttons on the tab bar, which is useful since UIViewControllers have a tabBarItem property. That lets you store just an array of UIViewControllers in your controller, as opposed to arrays for both controllers and tab bar items.

Because I was using mostly custom classes I did this all programmatically, including creating, configuring and laying out the buttons on the tab bar.

Like I said, this is just my experience, hope it helps.

I did this recently in an application. There was so little "code" to it that there is barely anything to post. I did it as such:

Created a UIImageView (about 50 px high), and laid it out at the bottom of the screen. I filled it with a tab-bar look-alike image - i.e. some sort of grey gradient. I probably subclassed UIImageView - but you don't even have to do that.

I drew a bunch of buttons/icons - 37x37 each.

I placed a bunch of UIButtons in the "tab bar" - and made them "Custom" views, with the buttons/icons I had created.

I simply used the touchUpInside methods to make them do stuff.

When I needed to get fancy - I'd attach the buttons to my code via and IBOutlet, so I could "disable" them - and I'd draw a greyish "disabled state" image for them.

If you want the same functionality, I'd recommend using a UITabBarController, hide the tab bar itself, and then create your own navigation. I've done this a few times and it works quite nicely.

Use something like this to hide your tab bar and expand the content view.

- (void)makeTabBarHidden:(BOOL)hide
{
    if ( [tabBarController_.view.subviews count] < 2 )
        return;
    UIView *contentView;
    if ( [[tabBarController_.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] )
        contentView = [tabBarController_.view.subviews objectAtIndex:1];
    else
        contentView = [tabBarController_.view.subviews objectAtIndex:0];
    if (hide)
    {
        contentView.frame = tabBarController_.view.bounds;      
    }
    else
    {
        contentView.frame = CGRectMake(tabBarController_.view.bounds.origin.x,
                                       tabBarController_.view.bounds.origin.y,
                                       tabBarController_.view.bounds.size.width,
                                       tabBarController_.view.bounds.size.height - tabBarController_.tabBar.frame.size.height);
    }
    tabBarController_.tabBar.hidden = hide;
}

Then make your own navigation with UIButtons to switch between your view controllers.

[tabBarController_ setSelectedIndex:0];

I think this is what you are looking for... should be pretty easy to subclass it. gl and cheer!

http://www.rumexit.co.uk/2010/07/how-to-customise-the-tab-bar-uitabbar-in-an-iphone-application-part-1-of-2/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!