iOS7 - Change UINavigationBar border color

后端 未结 15 2044
时光取名叫无心
时光取名叫无心 2020-12-07 14:44

Is it possible to change the grey border-bottom color of the UINavigationBar in iOS7?

I already tried to remove to border, but this is not working:

[         


        
15条回答
  •  南方客
    南方客 (楼主)
    2020-12-07 15:28

    Well, if you want to remove bottom border you set shadow image to empty image

    [navigationBar setShadowImage:[UIImage new]];
    

    so if you want to set it to another color just create image with that color, I use a helper function to create image from color below (original source http://jslim.net/blog/2014/05/05/ios-customize-uitabbar-appearance/)

    + (UIImage *)imageFromColor:(UIColor *)color forSize:(CGSize)size 
    {
    return [UIImage imageFromColor:color forSize:size withCornerRadius:0];
    }
    
    + (UIImage *)imageFromColor:(UIColor *)color forSize:(CGSize)size     withCornerRadius:(CGFloat)radius
    {
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    // Begin a new image that will be the new image with the rounded corners
    // (here with the size of an UIImageView)
    UIGraphicsBeginImageContext(size);
    
    // Add a clip before drawing anything, in the shape of an rounded rect
    [[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius] addClip];
    // Draw your image
    [image drawInRect:rect];
    
    // Get the image, here setting the UIImageView image
    image = UIGraphicsGetImageFromCurrentImageContext();
    
    // Lets forget about that we were drawing
    UIGraphicsEndImageContext();
    
    return image;
    }
    

    and in my navbar

    [navigationBar setShadowImage:[UIImage imageFromColor:[UIColor redColor] forSize:CGSizeMake(CGRectGetWidth(self.tableView.frame), 1)]];
    

    that's it, It's working for me, hope this help. Please consider changing the accepted answer because its not working and can be confusing

提交回复
热议问题