Iphone Custom UITabBarItem without rounded edges

2020-05-16 09:42发布

问题:

i try to customize a uitabbar

i extended uitabbar item and now have a customized image in it but i cant get rid of the rounded edges.

code:

@interface CustomTabBarItem : UITabBarItem  
{
    UIImage *customHighlightedImage;
}

@property (nonatomic, retain) UIImage *customHighlightedImage;

@end

@implementation CustomTabBarItem

@synthesize customHighlightedImage;

- (void) dealloc
{
    [customHighlightedImage release]; customHighlightedImage=nil;
    [super dealloc];
}

-(UIImage *) selectedImage
{
    return self.customHighlightedImage;
}

@end

maybe somoen knows how to get rid of the rounded rect

around the image

thanks in advance alex

回答1:

This is dirty - but works and got approved:

  • resizes tabbar
  • use your own images in own size

in the tab controller setup

    tabController   = [[UITabBarController alloc] init];
tabController.view.frame = CGRectMake(0, 72, 320, 480 - (82));
tabController.delegate = self;
UIImageView *bgImageView;
bgImageView = [ [ UIImageView alloc ] initWithImage: [UIImage imageNamed:TABBAR_BACKGROUND]];
bgImageView.frame = CGRectMake(0, -11, 320, 60);

[[tabController tabBar] addSubview:bgImageView];
[[tabController tabBar] sendSubviewToBack:bgImageView];
tabController.tabBar.frame = CGRectMake(0, 460 - (59 + 52 - 11), 320, 49);
[bgImageView release];

[window addSubview:tabController.view];

in the tabviewcontroller1 init method

   - (id) init
{
    if(self = [super init])
    {       
        CustomTabBarItem *tabItem = [[CustomTabBarItem alloc]
                                     initWithTitle:@"" image:nil tag:0];

        tabItem.customHighlightedImage=[UIImage imageNamed:TABBAR_TAB_1_ACTIVE];
        tabItem.customStdImage=[UIImage imageNamed:TABBAR_TAB_1_DEFAULT];       

        self.tabBarItem=tabItem;
        [tabItem release]; 
        tabItem=nil;
    }

return self;
}

and the custom tab bar it looks like

    @interface CustomTabBarItem : UITabBarItem  
    {
        UIImage *customHighlightedImage;
        UIImage *customStdImage;
    }

    @property (nonatomic, retain) UIImage *customHighlightedImage;
    @property (nonatomic, retain) UIImage *customStdImage;

    @end

#import "CustomTabBarItem.h"


@implementation CustomTabBarItem

@synthesize customHighlightedImage;
@synthesize customStdImage;

- (void) dealloc
{
    [customHighlightedImage release]; customHighlightedImage=nil;
    [customStdImage release]; customStdImage=nil;   
    [super dealloc];
}

-(UIImage *) selectedImage
{
    return self.customHighlightedImage;
}

-(UIImage *) unselectedImage
{
    return self.customStdImage;
}

@end

IMPORTANT:

i'm pretty new to iphone development and pretty pretty shure you can do this way less hacky. furthermore i got approved with this which does NOT mean you autmoatically will, too.



回答2:

thanks solved it with custom tab bar items

NOT APPLE APPROVED YET.

goes into tabController1.m

    - (id) init
{   
    if(self = [super init])
    {
        CustomTabBarItem *tabItem = [[CustomTabBarItem alloc]
                                     initWithTitle:@"" image:nil tag:0];

        tabItem.customHighlightedImage=[UIImage imageNamed:TABBAR_TAB_4_ACTIVE];
        tabItem.customStdImage=[UIImage imageNamed:TABBAR_TAB_4_DEFAULT];       

        self.tabBarItem=tabItem;
        [tabItem release]; 
        tabItem=nil;    
    }
    return self;
}

cutom tabbaritem:

@interface CustomTabBarItem : UITabBarItem  
{
    UIImage *customHighlightedImage;
    UIImage *customStdImage;
}

@property (nonatomic, retain) UIImage *customHighlightedImage;
@property (nonatomic, retain) UIImage *customStdImage;

@end

#import "CustomTabBarItem.h"


@implementation CustomTabBarItem

@synthesize customHighlightedImage;
@synthesize customStdImage;

- (void) dealloc
{
    [customHighlightedImage release]; customHighlightedImage=nil;
    [customStdImage release]; customStdImage=nil;   
    [super dealloc];
}

-(UIImage *) selectedImage
{
    return self.customHighlightedImage;
}

-(UIImage *) unselectedImage
{
    return self.customStdImage;
}

@end


回答3:

Set the cornerRadius on the view that has rounded corners to 0:

view.layer.cornerRadius = 0;

Also, you will probably need to add a #include to get the CALayer declarations:

#import <QuartzCore/QuartzCore.h>


回答4:

I have a query in the above implementation.

As per apple we should not use private / un-documented API's,

In the above code, the two methods

-(UIImage *) selectedImage {
    return self.customHighlightedImage; }

-(UIImage *) unselectedImage {
    return self.customStdImage; }

These methods were not defined in the custom subclass CustomTabBarItem.

These methods are un-documented / hidden methods in UITabBarItem class and overridden in the CustomTabBarItem class.

Is it fine to over-ride the undocumented methods?

I am still surprised how this got approved by Apple. I need some clarifications here.



回答5:

Any other apps validated by Apple with this code ? Very interested to know if we are authorized to use selectedImage and unselectedImage methods ?