2014-12-02 87 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