2014-12-02 88 views
8

在我的应用程序委托的didFinishLaunchingWithOptions函数中,我尝试自定义导航栏的外观。如何为UINavigationBar设置不透明的1px高阴影图像?

[UINavigationBar appearance].translucent = NO; 
[[UINavigationBar appearance] 
    setBackgroundImage:[UIImage 
     imageWithColor:[UIColor whiteColor] 
     size:CGSizeMake(1.0f, 1.0f) 
    ] 
    forBarMetrics:UIBarMetricsDefault 
]; 
[UINavigationBar appearance].shadowImage = [UIImage 
    imageWithColor:[UIColor redColor] 
    size:CGSizeMake(0.5f, 0.5f) 
]; 

我期待1px高的不透明红色阴影。相反,我得到了2px高的半透明红色阴影。如何使它看起来完全如我所愿?我已经完成了UITabBar的类似外观设置。另一方面,它表现得很好。

enter image description here

创建动态影像类别函数定义为这样:

+ (UIImage*)imageWithColor:(UIColor *)color size:(CGSize)size 
{ 
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height); 
    UIGraphicsBeginImageContext(rect.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetFillColorWithColor(context, [color CGColor]); 
    CGContextFillRect(context, rect); 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return image; 
} 
+0

你看着只是增加一个阴影的CALayer的?请参阅http://stackoverflow.com/questions/805872/how-do-i-draw-a-shadow-under-a-uiview – cmyr 2014-12-02 20:23:12

+0

@cmyr'[UINavigationBar appearance] .layer.borderWidth = 0.5f; [UINavigationBar外观] .layer.borderColor = [UIColor redColor] .CGColor;'在导航栏下不显示任何内容。 – Pwner 2014-12-02 21:55:34

+0

您是否将图层的clipsToBounds设置为NO? – cmyr 2014-12-02 23:21:47

回答

-1

呃...这是你想要的吗?

screenshot

在我的视图控制器,我只是简单地添加一个UIView为1米像素的高度和偏移44pixels的:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    self.view.backgroundColor = [UIColor whiteColor]; 

    [self setupNavBar]; 
} 

-(void)setupNavBar 
{ 
    self.navigationItem.title = @"Contacts"; 

    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame]; 

    UIView *bar = [[UIView alloc] initWithFrame:CGRectMake(0, 44, applicationFrame.size.width, 1.0)]; 
    bar.backgroundColor = [UIColor redColor]; 

    [self.navigationController.navigationBar addSubview:bar]; 
} 

你可以改变任何你想要的颜色。

+2

恕我直言,我认为这不是向导航栏添加子视图的最佳解决方案。看起来很容易出错(例如,当多个视图控制器试图添加视图时,或者 - 当导航栏的视图层次可能随iOS的更新版本而改变时)。我认为使用公开可用的功能,自定义背景图像和UIAppearance设置会更安全。 – auco 2015-06-25 15:33:51

15

你需要创建一个是视网膜感知一个图形上下文:

let pixelScale = UIGraphicsBeginImageContextWithOptions(fillRect.size, false, UIScreen.main.scale) 

之后,你的影子图像将成为1个物理像素高。

这里是全码:

extension UIImage { 

    static func imageWithColor(color: UIColor) -> UIImage { 
     let pixelScale = UIScreen.main.scale 
     let pixelSize = 1/pixelScale 
     let fillSize = CGSize(width: pixelSize, height: pixelSize) 
     let fillRect = CGRect(origin: CGPoint.zero, size: fillSize) 
     UIGraphicsBeginImageContextWithOptions(fillRect.size, false, pixelScale) 
     let graphicsContext = UIGraphicsGetCurrentContext() 
     CGContextSetFillColorWithColor(graphicsContext, color.CGColor) 
     CGContextFillRect(graphicsContext, fillRect) 
     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return image 
    } 

} 

在GitHub上:

https://github.com/salutis/swift-image-with-color

+0

我建议填充大小{1,pixelSize}而不是{pixelSize,pixelSize},否则它会创建一个水平渐变效果,并且在我的情况下,该线条在右点处变得透明。 – 2017-05-29 12:04:33