UItabBar設置的一些設置 合一些效果實現 設置badgeValue的方法封裝

ios默認的樣式不太適合我們的應用,可以通過一下方法設置相關屬性: 
設置背景: 
[_tabBar setBackgroundImage:[UIImage imageNamed:@"bg_tabbar"]];

設置某個Item選中的效果: 
_tabBar.selectionIndicatorImage = [UIImage imageNamed:@"bar_item_selected"];//設置選中效果圖片 

設置UITabBarItem文字顏色 
[[UITabBarItem appearance] setTitleTextAttributes:@{ UITextAttributeTextColor : [UIColor whiteColor] } 
forState:UIControlStateNormal]; 
[[UITabBarItem appearance] setTitleTextAttributes:@{ UITextAttributeTextColor : [UIColor whiteColor] } 
forState:UIControlStateHighlighted]; 

設置UITabBarItem未選中與選中時的圖片: 

[_hotTabItem setFinishedSelectedImage:[UIImage imageNamed:@"1_selected"] withFinishedUnselectedImage:[UIImage imageNamed:@"1"]]; 
[_searchTabItem setFinishedSelectedImage:[UIImage imageNamed:@"2_selected"] withFinishedUnselectedImage:[UIImage imageNamed:@"2"]]; 
[_userTabItem setFinishedSelectedImage:[UIImage imageNamed:@"3_selected"] withFinishedUnselectedImage:[UIImage imageNamed:@"3"]]; 
上述方法基本可以滿足了。 
如果還不行可以自定義實現UITabBar。


    bottomBar = [[UITabBaralloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(sframe)-25,self.view.frame.size.width,0)];//初始隱藏bottomBar

    //[bottomBar setDelegate:self];

    [self.viewaddSubview:bottomBar];

   //按鈕

   CGFloat btnWidth = (sframe.size.width-165)/2;//兩個按鈕

    CGFloat btnTop =0;//sframe.size.height-barHeight;

   CGFloat _barHeight = dHeightBar;

   MU_tabButton* btn1 = [[MU_tabButtonalloc] initWithFrame:CGRectMake(0, btnTop, btnWidth, _barHeight)];

    

    [btn1 addTarget:selfaction:@selector(selectAllClicked:)forControlEvents:UIControlEventTouchUpInside];

    [btn1 setBgImg:dImgTabBgandIconImg:dImgSelectAllandTitle:@"全選"withColor:dColorTabTextNormalforState:UIControlStateNormal];

   MU_tabButton* btn2 = [[MU_tabButtonalloc] initWithFrame:CGRectMake(btnWidth, btnTop, btnWidth, _barHeight)];

    [btn2 setBackgroundImage:dImgTabClickedforState:UIControlStateHighlighted];

    [btn2 addTarget:selfaction:@selector(deleteClicked:)forControlEvents:UIControlEventTouchUpInside];

    [btn2 setBgImg:dImgTabBgandIconImg:dImgDeleteandTitle:@"刪除"withColor:dColorTabTextNormalforState:UIControlStateNormal];

    [bottomBaraddSubview:btn1];

    [bottomBaraddSubview:btn2];

///////////其中MU_tabButton 爲自定義類型

主要屬性包含如下:

- (id)initWithFrame:(CGRect)frame

{

   self = [superinitWithFrame:frame];

   if (self) {

        // Initialization code

       CGFloat iconHeight = frame.size.height*3/5;

       //文字

       titleLabel = [[UILabelalloc] initWithFrame:CGRectMake(0, iconHeight, frame.size.width, frame.size.height- iconHeight)];

        titleLabel.backgroundColor = [UIColorclearColor];

       titleLabel.textColor = [UIColorwhiteColor];

        titleLabel.textAlignment =NSTextAlignmentCenter;

       titleLabel.font = [UIFontsystemFontOfSize:13];

        [selfaddSubview:titleLabel];

       //圖標

       CGFloat iconGap = 3;

       CGFloat iconSize = iconHeight - iconGap;//圖標大小

       iconView = [[UIImageViewalloc] initWithFrame:CGRectMake((frame.size.width- iconSize)/2, iconGap, iconSize, iconSize)];

        [selfaddSubview:iconView];

    }

    return self;

}


-(void)setBgImg:(UIImage *)bgImg andIconImg:(UIImage *)img andTitle:(NSString *)title  withColor:(UIColor*)tColor forState:(UIControlState)state

{

    [selfsetBackgroundImage:bgImg forState:UIControlStateNormal];

   iconView.image = img;

   titleLabel.text = title;

   titleLabel.textColor = tColor;

}



-(void)setBadgeValue:(NSString*)val atTabIndex:(int)index

{

    //   UITabBarItem* tab = [[bottomBtnBar items] objectAtIndex:2*index];

    UITabBarItem* tab = [[bottomBtnBar items] objectAtIndex:index];

    if ([val integerValue] <= 0) {

        tab.badgeValue = nil;

    }

    else

    {

        tab.badgeValue = val;

    }

}


-(NSString*)getBadgeValueAtIndex:(int)index

{

    UITabBarItem* tab = [[bottomBtnBar items] objectAtIndex:index];

    return tab.badgeValue;

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章