定制使UITabBarController的tabbar选中tabbaritem时不高亮以及换背景色(支持iPad旋转)/IOS5&IOS4

(注:IOS5有新的API可以实现这些功能,这里这样搞是为了支持IOS4.)

/*
 * @brief 定制的tabbar.
 */

#import <UIKit/UIKit.h>

@interface XUTabBarController : UITabBarController
{
@private
    UIView * bgView4TabBar_;
}

@end

#import "XUTabBarController.h"


@interface XUTabBarController()
@property(nonatomic, retain) UIView * bgView4TabBar;
- (void)customTabBarBackgroundColor;
- (void)setNoHighlightTabBar;
@end

@implementation XUTabBarController
@synthesize bgView4TabBar = bgView4TabBar_;

- (void)dealloc
{
    [bgView4TabBar_ release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self customTabBarBackgroundColor];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.bgView4TabBar = nil;
}

#pragma mark - For iPad Rotate

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if(UIU_IsIPad)
    {
        CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
        CGRect frame = self.tabBar.frame;
        frame.origin.x = 0;
        frame.origin.y = 0;
        frame.size.width = (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft 
                            || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
        ? /*横屏*/ appFrame.size.height + 20 
        : appFrame.size.width + 20;
        self.bgView4TabBar.frame = frame;
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //
    [self willRotateToInterfaceOrientation:self.interfaceOrientation duration:0];
}

#pragma mark - NoHighlightTab

- (void)setSelectedIndex:(NSUInteger)index
{
    [super setSelectedIndex:index];
    [self setNoHighlightTabBar];
}

- (void)setSelectedViewController:(UIViewController *)selectedViewController
{
    [super setSelectedViewController:selectedViewController];
    [self setNoHighlightTabBar];
}

- (void)setNoHighlightTabBar
{
    int tabCount = [self.viewControllers count] > 5 ? 5 : [self.viewControllers count];
    NSArray * tabBarSubviews = [self.tabBar subviews];
    for(int i = [tabBarSubviews count] - 1; i > [tabBarSubviews count] - tabCount - 1; i--)
    {
        for(UIView * v in [[tabBarSubviews objectAtIndex:i] subviews])
        {
            if(v && [NSStringFromClass([v class]) isEqualToString:@"UITabBarSelectionIndicatorView"])
            {//the v is the highlight view.
                [v removeFromSuperview];
                break;
            }
        }
    }
}

#pragma mark - Custom TabBar Background Color

- (void)customTabBarBackgroundColor
{
    ///给tabbar换背景色
    //
    //frame
    CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
    CGRect frame = self.tabBar.frame;
    frame.origin.x = 0;
    frame.origin.y = 0;
    frame.size.width = (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft 
                        || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
    ? /*横屏*/ appFrame.size.height + 20 
    : appFrame.size.width + 20; 

    //bg color.
    #define useTintColor 1
    #if useTintColor
    self.bgView4TabBar = [[[UINavigationBar alloc] initWithFrame:frame] autorelease];
    ((UINavigationBar *)self.bgView4TabBar).tintColor = XX_TabBgColor;    
    #else
    self.bgView4TabBar = [[[UIView alloc] initWithFrame:frame] autorelease];
    self.bgView4TabBar.backgroundColor = XX_TabBgColor;
    #endif
    //add to.
    [self.tabBar insertSubview:self.bgView4TabBar atIndex:
     self.tabBar.subviews.count - (self.viewControllers.count > 5 ? 5 : self.viewControllers.count)
     ];
}

@end

转贴: http://www.cnblogs.com/xiaouisme/archive/2012/03/21/2410820.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值