How to customize the appearance of a tab bar?

帅比萌擦擦* 提交于 2020-01-05 08:26:51

问题


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

  • 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?


回答1:


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!

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.

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.



来源:https://stackoverflow.com/questions/2778466/how-to-customize-the-appearance-of-a-tab-bar

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