如何创建自己的 UITabBar?

发布于 2024-09-29 05:22:48 字数 1436 浏览 1 评论 0原文

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

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

发布评论

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

评论(4

冧九 2024-10-06 05:22:48

我不知道是否有最好的方法来做到这一点,但我将分享我的经验:

我通过子类化 UIView 而不是 UITabBar 创建了我自己的选项卡栏版本。正如链接的问题所提到的,Apple 的 UI 类对其大小非常挑剔,我认为我无法将 UITabBar 子类设置为我想要的大小。

因为我没有 UITabBar 的子类,所以我最终也推出了自己版本的 UITabBarController 和 UITabBarDelegate,其接口本质上与 Apple 类相同。我能够使用 UITabBarItem 来存储选项卡栏上按钮的标题和图标,这很有用,因为 UIViewControllers 有一个 tabBarItem 属性。这使您可以在控制器中仅存储 UIViewController 数组,而不是同时存储控制器和选项卡栏项目的数组。

因为我主要使用自定义类,所以我以编程方式完成了这一切,包括创建、配置和布置选项卡栏上的按钮。

正如我所说,这只是我的经验,希望对您有所帮助。

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.

浅语花开 2024-10-06 05:22:48

我最近在一个应用程序中这样做了。它的“代码”太少了,几乎没有什么可发布的。我是这样做的:

创建了一个 UIImageView(大约 50 px 高),并将其放置在屏幕底部。我用标签栏相似的图像填充它 - 即某种灰色渐变。我可能对 UIImageView 进行了子类化 - 但你甚至不必这样做。

我画了一堆按钮/图标 - 每个 37x37。

我在“选项卡栏”中放置了一堆 UIButton,并使用我创建的按钮/图标将它们设为“自定义”视图。

我只是使用 touchUpInside 方法让它们做一些事情。

当我需要变得更奇特时 - 我会通过 IBOutlet 将按钮附加到我的代码,这样我就可以“禁用”它们 - 并且我会为它们绘制一个灰色的“禁用状态”图像。

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.

污味仙女 2024-10-06 05:22:48

如果您想要相同的功能,我建议使用 UITabBarController,隐藏选项卡栏本身,然后创建您自己的导航。我已经这样做过几次并且效果非常好。

使用类似的方法来隐藏选项卡栏并展开内容视图。

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

然后使用 UIButtons 进行自己的导航以在视图控制器之间切换。

[tabBarController_ setSelectedIndex:0];

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];

凹づ凸ル 2024-10-06 05:22:48

我认为这就是您正在寻找的......应该很容易对其进行子类化。欢呼吧!

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

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/

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