如何将自定义分隔符图像添加到UITabBar?

时间:2012-09-24 20:09:01

标签: iphone ios uitabbarcontroller divider

我有一个UITabBar,我试图在新的iOS 6 App Store外观后设置样式。我有一些非常漂亮的渐变,但我想知道如何设置分频器图像。

除了手动将线条添加到每个图像之外,我还能做些什么来重现分割器外观吗?

enter image description here

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:3)

您可以为所选项目和未选择状态添加每个项目的图像。 像这样:

    UIImage *selectedImage0     = [UIImage imageNamed:@"image1.png"];
    UIImage *unselectedImage0   = [UIImage imageNamed:@"image1_unselected.png"];

    UIImage *selectedImage1     = [UIImage imageNamed:@"image2.png"];
    UIImage *unselectedImage1   = [UIImage imageNamed:@"image2_unselected.png"];

    UIImage *selectedImage2     = [UIImage imageNamed:@"image3.png"];
    UIImage *unselectedImage2   = [UIImage imageNamed:@"image3_unselected.png"];

    UIImage *selectedImage3     = [UIImage imageNamed:@"image4.png"];
    UIImage *unselectedImage3   = [UIImage imageNamed:@"image4_unselected.png"];

    UIImage *selectedImage4     = [UIImage imageNamed:@"image5.png"];
    UIImage *unselectedImage4   = [UIImage imageNamed:@"image5_unselected.png"];

    UITabBar     *tabBar = self.tabBarController.tabBar;
    UITabBarItem *item0  = [tabBar.items objectAtIndex:0];
    UITabBarItem *item1  = [tabBar.items objectAtIndex:1];
    UITabBarItem *item2  = [tabBar.items objectAtIndex:2];
    UITabBarItem *item3  = [tabBar.items objectAtIndex:3];
    UITabBarItem *item4  = [tabBar.items objectAtIndex:4];

    [item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
    [item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:unselectedImage1];
    [item2 setFinishedSelectedImage:selectedImage2 withFinishedUnselectedImage:unselectedImage2];
    [item3 setFinishedSelectedImage:selectedImage3 withFinishedUnselectedImage:unselectedImage3];
    [item4 setFinishedSelectedImage:selectedImage4 withFinishedUnselectedImage:unselectedImage4];

您可以将此代码放在任何控制器的viewDidLoad方法中。 希望这会有所帮助。

干杯!

答案 1 :(得分:2)

我把我的标签栏图像如下: -

my tab bar image

我将它设置在自来水栏上,如: -

  CGRect frame = CGRectMake(0.0, 0.0, 320, 48);
        NSLog(@"%f",frame.size.width);
        UIView *v = [[UIView alloc] initWithFrame:frame];
        // [v setBackgroundColor:DARK_BACKGROUNDTabBar];
        UIImageView *imgView =[[UIImageView alloc]initWithFrame:frame];
        UIImage *img1 = [UIImage imageNamed:@"tabbar.png"];
        [imgView setImage:img1];
        [v addSubview:imgView];

        [v setAlpha:1];
        [[self.tabBarController tabBar] insertSubview:v atIndex:1];
        [v release];
希望它能为你充分利用

您可以使用4或5个标签创建自己的图像,并设置如上

答案 2 :(得分:1)

如果您想要的是为选项卡提供“选定”和“正常”背景图像(包括左右分隔符的一半),您可以继承UITabBarController,并在viewDidLoad中用UIButton替换UITabBar。这很直接。图像被设置为按钮的背景。它们在左侧和右侧包含4px的分隔线。

我在故事板中使用此功能,因此在调用 viewDidLoad 时已填充 self.tabBar.items 数组。

@interface MyTabBarController()
@property (nonatomic,strong) UIImage *selectedImage;
@property (nonatomic,strong) UIImage *normalImage;
@end

@implementation MyTabBarController
@synthesize selectedImage, normalImage;

- (void) viewDidLoad
{
    [super viewDidLoad];

    // set whichever view you want to start with
    self.selectedIndex = 1;

    self.tabBar.hidden=YES;

    // those are the images used for the selected and not selected tabs
    self.selectedImage = [[UIImage imageNamed:@"tabBarBackground_down.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:0];
    self.normalImage = [[UIImage imageNamed:@"tabBarBackground.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:0];

    CGFloat width = self.view.frame.size.width/[self.tabBar.items count];
    for( int itemIndex=0; itemIndex<[self.tabBar.items count]; itemIndex++ )
    {
        UITabBarItem *item = [self.tabBar.items objectAtIndex:itemIndex];

        // create a button
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

        // configure the button to act as a tab
        button.adjustsImageWhenHighlighted = NO;
        [button setImage:item.image forState:UIControlStateNormal];
        [button setBackgroundImage:self.selectedIndex==itemIndex?self.selectedImage:self.normalImage forState:UIControlStateNormal];

        // this is used later to set the selectedIndex of the UITabBarController
        button.tag = itemIndex;

        // position the button in place of tab
        CGFloat nextXStart = itemIndex+1==[self.tabBar.items count]?self.view.frame.size.width:rint(width*(itemIndex+1));
        button.frame = CGRectMake(rint(width*itemIndex), self.view.frame.size.height-49, nextXStart-rint(width*itemIndex) , 49);

        [self.view addSubview:button];

        // add event to the button
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
    }
}

- (void) buttonClicked:(id)sender
{
    UIButton *button = sender;
    if( self.selectedIndex!=button.tag )
    {
        // select the tab and change the visible viewcontroller
        self.selectedIndex = button.tag;

        // reset all but the active buttons to have the normal image
        for( UIView *subview in self.view.subviews )
        {
            if( subview!=sender && [subview isKindOfClass:[UIButton class]])
                [((UIButton *)subview) setBackgroundImage:self.normalImage  forState:UIControlStateNormal];
        }

        // set the selectedImage as background of the clicked button
        [button setBackgroundImage:self.selectedImage  forState:UIControlStateNormal];
    }
}
@end