2017-07-01 32 views
0

i want like this underline when different tab enter image description here如何當的UITabBarController

UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake([UITabBar appearance].frame.origin.x,[UITabBar appearance].frame.origin.y, [[UIScreen mainScreen] bounds].size.width/5, 56)];  
    UIImageView *border = [[UIImageView alloc]initWithFrame:CGRectMake(view1.frame.origin.x,view1.frame.size.height-6, [[UIScreen mainScreen] bounds].size.width/5, 6)]; 
    border.backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:105.0f/255.0f blue:84.0f/255.0f alpha:1.0f]; 
    [view1 addSubview:border]; 
    UIImage *img=[self ChangeViewToImage:view1]; 
    [[UITabBar appearance] setSelectionIndicatorImage:img]; 
    [[UITabBar appearance] setTintColor: [UIColor colorWithRed:255.0f/255.0f green:105.0f/255.0f blue:84.0f/255.0f alpha:1.0f]]; 

回答

1

AppDelegatedidFininshLaunch方法添加全局樣式UITabbar

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake([UITabBar appearance].frame.origin.x,[UITabBar appearance].frame.origin.y, [[UIScreen mainScreen] bounds].size.width/3, 56)]; 
    UIImageView *border = [[UIImageView alloc]initWithFrame:CGRectMake(view1.frame.origin.x,view1.frame.size.height-6, [[UIScreen mainScreen] bounds].size.width/3, 6)]; 
    border.backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:105.0f/255.0f blue:84.0f/255.0f alpha:1.0f]; 
    [view1 addSubview:border]; 
    UIImage *img=[self ChangeViewToImage:view1]; 
    [[UITabBar appearance] setSelectionIndicatorImage:img]; 
    [[UITabBar appearance] setTintColor: [UIColor colorWithRed:255.0f/255.0f green:105.0f/255.0f blue:84.0f/255.0f alpha:1.0f]]; 

    return YES; 
} 
- (UIImage *)ChangeViewToImage:(UIView *) viewForImage{ 
    UIGraphicsBeginImageContext(viewForImage.bounds.size); 
    [viewForImage.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return img; 
} 

UPDATE選擇不同的選項卡具有不同的顏色添加下劃線: 要設置自定義指標線

CAShapeLayer rectShape; 
const CGFloat indicatorHeight = 5; 
CGFloat indicatorWidth; 
const CGFloat indicatorBottomMargin = 2; 
const CGFloat indicatorLeftMargin = 2; 

@implementation ViewController 

-(void) viewDidLoad{ 
    rectShape.fillColor = [UIColor redColor].CGColor; 
    indicatorWidth = view.bounds.maxX /2; //Count of items 
    [self.tabBarController.view.layer addSublayer: rectShape]; 
    self.tabBarController.delegate = self; 

    //set Initial position 
    [self updateTabbarIndicatorWithSelectedIndex: 0]; 
} 

-(void)updateTabbarIndicatorWithSelectedIndex:(NSInteger) index{ 
    CGRect updatedBounds = CGRectMake((CGFloat)index * (indicatorWidth + indicatorLeftMargin), 
             view.bounds.maxY - indicatorHeight, 
             indicatorWidth - indicatorLeftMargin, 
             indicatorHeight); 
    CGMutablePathRef path; 
    CGPathAddRect(path, nil, updatedBounds); 
    rectShape.path = path; 
} 

-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{ 
    if(tabBarController.selectedIndex == 1){ 
     //customize your color 
     rectShape.fillColor = [UIColor blueColor].CGColor; 
    } 
    [self updateTabbarIndicatorWithSelectedIndex: tabBarController.selectedIndex]; 
} 
+0

謝謝你的回答,但我alreday嘗試過。對於所有標籤,它顯示單一顏色。但我想要不同的顏色爲不同的tabBarItems –

+0

我已經添加了新的方法,請檢查它。上面的代碼只是寫在沒有mac(在記事本中),所以請原諒我的錯誤,如果有的話。 –

相關問題