簡體   English   中英

在UITabBarController中選擇了不同選項卡時,如何添加具有不同顏色的下划線

[英]How to add Underline with different color when different tab is selected in 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]];

AppDelegate didFininshLaunch方法為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;
}

更新:設置自定義指標行

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];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM