开发者

How to change default the gray color of uitabbaritem in uitabbarcontroller?

In tabbarcontroller applic开发者_如何学Cation, all deselected image tabs have gray color, i want to change it into white.

any help will appreciate!


You will need to build your own controller. Per the Apple docs on the matter "This class [UITabBarController] is not intended for subclassing". The docs on the UITabBarItem say that when you are supplying an image for the tab bar "The images displayed on the tab bar are derived from this image". So, whatever image you provide to the tab bar will get manipulated to make it conform to the "normal" look of a tab bar image.

So, you can build a UIViewController with some UIButtons as subviews and then manage the entire look and feel that way.

IMHO, this seems like a lot of work for not a lot of gain.


check out https://github.com/xhan/PlutoLand and run it.

you can find a custom PLTabBarController class which allow you custom your TabBarItems there.


You may inherit from UITabBar and override drawRect method. Below is a sample how it is colored in brown

- (void)drawRect:(CGRect)rect 
{
    // Drawing code
    float baseComponents[] = { 78 / 255.0, 30 / 255.0, 0/ 255.0, 1.0 };

    // Get current context
    CGContextRef context = UIGraphicsGetCurrentContext();

    // Fill full rectangle
    CGContextSetRGBFillColor(context, baseComponents[0], baseComponents[1], baseComponents[2], baseComponents[3]);
    CGContextFillRect(context,rect);

    // Draw light line on top
    CGContextSetRGBStrokeColor(context, baseComponents[0] + (67/255.0), baseComponents[1] + (67/255.0), baseComponents[2] + (67/255.0),  baseComponents[3]);
    CGContextMoveToPoint(context, 0, 1);
    CGPoint points[] = { CGPointMake(0,1.5),CGPointMake(rect.size.width,1.5) };
    CGContextStrokeLineSegments(context, points , 2);

    // Create gradient
    CGColorSpaceRef myColorspace;
    CGGradientRef myGradient;
    size_t num_locations = 2;
    CGFloat locations[2] = { 0.0, 1.0 };
    CGFloat components[8] = { baseComponents[0] + (46/255.0), baseComponents[1] + (46/255.0), baseComponents[2] + (46/255.0),  baseComponents[3],  // Start color
    baseComponents[0] + (21/255.0), baseComponents[1] + (21/255.0), baseComponents[2] + (21/255.0),  baseComponents[3] }; // End color

    myColorspace = CGColorSpaceCreateDeviceRGB();
    myGradient = CGGradientCreateWithColorComponents (myColorspace, components,locations, num_locations);

    // Draw gradient
    CGContextDrawLinearGradient(context, myGradient, CGPointMake(0, 2), CGPointMake(0,rect.size.height/2), 0);

    // Clean up
    CGColorSpaceRelease(myColorspace);
    CGGradientRelease(myGradient);

}

Than in Interface Builder set your custom class for TabBar of UITabBarController in Identity Inspector.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜