Iphone Custom UITabBarItem without rounded edges

匆匆过客 提交于 2019-12-02 18:17:26

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.

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

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>

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.

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

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