【问题标题】:Add a simple CGGradient to UINavigationBar [duplicate]向 UINavigationBar 添加一个简单的 CGGradient [重复]
【发布时间】:2012-03-07 09:45:26
【问题描述】:

可能重复:
UINavigationBar gradient details

我浏览了网络,但没有找到一个明确的例子。我没有使用 CGGradient 的经验,但我需要一个非常简单的东西: 我想给我的 NavigationController 的 NavigationBar 添加一个渐变,渐变应该是线性的,从上到下。我希望起始颜色为:(88.0, 38.0, 40.0) 和结束颜色 (50.0 15.0 16.0)。请给我一个示例,说明如何将此渐变添加到导航栏?

谢谢

【问题讨论】:

    标签: iphone xcode ios5 linear-gradients


    【解决方案1】:

    根据需要调整颜色或提供图像

    //                                  #Lighter r,g,b,a                    #Darker r,g,b,a
    #define MAIN_COLOR_COMPONENTS       { 0.153, 0.306, 0.553, 1.0, 0.122, 0.247, 0.482, 1.0 }
    #define LIGHT_COLOR_COMPONENTS      { 0.478, 0.573, 0.725, 1.0, 0.216, 0.357, 0.584, 1.0 }
    
    @implementation UINavigationBar (UINavigationBarCategory)
    
    - (void)drawRect:(CGRect)rect {
        if (imageReady) {
            UIImage *img = [UIImage imageNamed: @"navigation_background.png"];
            [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        } else {
            // Render yourself instead.
            // You will need to adjust the MAIN_COLOR_COMPONENTS and LIGHT_COLOR_COMPONENTS to match your app
    
           // emulate the tint colored bar
           CGContextRef context = UIGraphicsGetCurrentContext();
           CGFloat locations[2] = { 0.0, 1.0 };
           CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB();
    
           CGFloat topComponents[8] = LIGHT_COLOR_COMPONENTS;
           CGGradientRef topGradient = CGGradientCreateWithColorComponents(myColorspace, topComponents, locations, 2);
           CGContextDrawLinearGradient(context, topGradient, CGPointMake(0, 0), CGPointMake(0,self.frame.size.height/2), 0);
           CGGradientRelease(topGradient);
    
           CGFloat botComponents[8] = MAIN_COLOR_COMPONENTS;
           CGGradientRef botGradient = CGGradientCreateWithColorComponents(myColorspace, botComponents, locations, 2);
           CGContextDrawLinearGradient(context, botGradient,
           CGPointMake(0,self.frame.size.height/2), CGPointMake(0, self.frame.size.height), 0);
           CGGradientRelease(botGradient);
    
           CGColorSpaceRelease(myColorspace);
    
    
           // top Line
           CGContextSetRGBStrokeColor(context, 1, 1, 1, 1.0);
           CGContextMoveToPoint(context, 0, 0);
           CGContextAddLineToPoint(context, self.frame.size.width, 0);
           CGContextStrokePath(context);
    
           // bottom line
           CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0);
           CGContextMoveToPoint(context, 0, self.frame.size.height);
           CGContextAddLineToPoint(context, self.frame.size.width, self.frame.size.height);
           CGContextStrokePath(context);
        }
    }
    

    source

    【讨论】:

      【解决方案2】:

      以下是用于设置导航图像的渐变。

      @interface CustomNavigationBar : UINavigationBar
      @end
      
      @implementation CustomNavigationBar
      -(void) drawRect:(CGRect)rect 
      {
          UIImage *image = [UIImage imageNamed: @"myNavBarImage"];
          [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
      }
      @end
      
      // this can go anywhere
      +(UINavigationController*) myCustomNavigationController
      {
        MyViewController *vc = [[[MyViewController alloc] init] autorelease];
        UINavigationController *nav = [[[NSBundle mainBundle] loadNibNamed:@"CustomNavigationController" owner:self options:nil] objectAtIndex:0];
        nav.viewControllers = [NSArray arrayWithObject:vc];
        return nav;
      }
      

      Also you can download the source for this kind implemented demo

      感谢和问候

      霓虹灯塞缪尔

      【讨论】:

      • 我想以编程方式创建渐变,而不是使用图像作为背景。
      【解决方案3】:

      使用 tintColor 属性,

      [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
      

      一切正常。想知道您是否需要进一步说明

      【讨论】:

        猜你喜欢
        • 2018-06-18
        • 2017-01-14
        • 1970-01-01
        • 2014-03-05
        • 1970-01-01
        • 2015-10-26
        • 2011-05-26
        • 2012-06-24
        相关资源
        最近更新 更多