11

我有一个 UITabBar,我正在尝试按照新的 iOS 6 App Store 外观设计样式。我有一些非常好看的渐变,但我想知道如何设置分隔线图像。

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

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

4

3 回答 3

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任何控制器的方法中。希望这可以帮助。

干杯!

于 2012-09-24T20:22:11.193 回答
2

我把我的标签栏图片像: -

我的标签栏图片

我将它设置在点击栏上,例如:-

  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 个选项卡创建自己的图像并像上面那样设置

于 2012-09-25T09:18:24.847 回答
1

如果您只想为选项卡提供“选定”和“正常”背景图像(包括左右分隔线的一半),您可以将 UITabBarController 子类化,并在 viewDidLoad 期间将 UITabBar 替换为 UIButtons。这很简单。图像被设置为按钮的背景。它们在左侧和右侧包含 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
于 2012-11-24T07:27:22.633 回答