如何自定义选项卡栏的外观? [英] How to customize the appearance of a tab bar?

查看:117
本文介绍了如何自定义选项卡栏的外观?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自定义标签栏的外观。具体来说,我想要:

I am looking to customize the appearance of a tab bar. Specifically I want to:


  • 更改默认的tabBar颜色

  • tabBar

  • 将自定义图片添加到tabBarButtons

  • 更改tabBarButtons的字体

  • Change the default tabBar color
  • Add a custom image on top of the tabBar
  • Add custom images to tabBarButtons
  • Change the font of tabBarButtons

简而言之,我想要一个标签栏的所有功能,但完全自定义外观。

In a nutshell, I want all of the functionality of a tab bar but with a completely custom look.

我应该开始对子元素进行子类化还是使用类别或什么?

Should I start subclassing elements, or using categories or what?

推荐答案

当面对这种情况时,我刚刚编写了一个自定义类(子类化UIViewController)和使用按钮作为用户交互的UI对象。使用这种技术,你可以非常积极的内存管理,删除和重新创建未使用的选项卡,UITabViewController不能管理的方式。我没有覆盖的唯一的区域是更多...交换到表视图 - 但后来我不想要这真的!

When faced with this situation I've just written a custom class (subclassing UIViewController) and used buttons as the UI object the user interacts with. With this technique you can be pretty aggressive in memory management, dropping and recreating unused tabs in a way that the UITabViewController doesn't manage. The only area I didn't cover was the MORE...swap into a table view - but then I didn't want that either really!

编辑...
没有可重用的代码。对于每个标签只需使用自定义按钮的样式,你想要的方式。我有三个状态为每个按钮正常(取消选择),按下(打开)和电流(关闭)。我的touchupinside处理程序通过调用greytherightbuttons并传递按钮的标签来根据需要切换正常和按下的按钮。

EDIT... There is no reusable code as such. For each tab just use a custom button styled in the way you want. I've got three states for each button normal(deselect), pressed(on) and current(off). My touchupinside handler swaps out the normal and pressed buttons as required by calling greytherightbuttons and passing the tag of the button.


- (void) greytherightbuttons:(int)n {


    switch (n) {
        case 0:
            [b0 setImage:[UIImage imageNamed:@"btn_gallery_your_designs_off.png"] forState:UIControlStateNormal];
            [b1 setImage:[UIImage imageNamed:@"btn_gallery_recent_editions_deselect.png"] forState:UIControlStateNormal];
            [b2 setImage:[UIImage imageNamed:@"btn_gallery_highest_rated_deselect.png"] forState:UIControlStateNormal];
            break;
        case 1:
            [b0 setImage:[UIImage imageNamed:@"btn_gallery_your_designs_deselect.png"] forState:UIControlStateNormal];
            [b1 setImage:[UIImage imageNamed:@"btn_gallery_recent_editions_off.png"] forState:UIControlStateNormal];
            [b2 setImage:[UIImage imageNamed:@"btn_gallery_highest_rated_deselect.png"] forState:UIControlStateNormal];         
            break;

        case 2:
            [b0 setImage:[UIImage imageNamed:@"btn_gallery_your_designs_deselect.png"] forState:UIControlStateNormal];
            [b1 setImage:[UIImage imageNamed:@"btn_gallery_recent_editions_deselect.png"] forState:UIControlStateNormal];
            [b2 setImage:[UIImage imageNamed:@"btn_gallery_highest_rated_off.png"] forState:UIControlStateNormal];

            break;
        default:
            break;
    }

}

下一步是交换正确的视图。

The next step is to swap in the correct view.

在视图中切换的实际方式将取决于你想要创建视图,但它没有比alloc / initWithFrame更复杂的新视图控制器。 [self.view addSubview:newvc.view],然后将其从视图中删除,并在更改标签页时将其删除。

The actual way you swap in the view will depend on how you want to create the views, but its no more complicated than alloc/initWithFrame your new view controller. [self.view addSubview:newvc.view] then remove it from the view and nil it when you change tabs.

这篇关于如何自定义选项卡栏的外观?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆