(原) 定製使UITabBarController的tabbar選中tabbaritem時不高亮以及換背景色(支援iPad旋轉)

來源:互聯網
上載者:User

(註: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.