UINavigationBar自定义颜色细线边框

时间:2018-05-21 02:22:12

标签: ios uinavigationbar uiappearance

首先,在发布此问题之前我确实搜索了这个问题,但是如果有答案,则会将其隐藏在数百万关于如何删除的问题之下导航栏的默认底部边框。

我不想删除导航栏的底部边框(“阴影”)。

我试图通过使用外观代理的常用方法来“主题化”我的应用程序。 我可以使用以下代码全局更改UINavigationBar的大多数可视属性:

let navigationBarProxy = UINavigationBar.appearance()
navigationBarProxy.isTranslucent = false
navigationBarProxy.barTintColor = myBarBackgroundColor
navigationBarProxy.tintColor = myBarTextColor

关于条形图的“细线”底部边框(或者已知的“阴影”),我可以通过不执行任何操作或指定nil来设置默认值:

navigationBarProxy.shadowImage = nil

...或者我可以指定自定义颜色,方法是指定我之后颜色的实体图像

navigationBarProxy.shadowImage = UIImage.withColor(myBorderColor)

(使用帮助扩展名:)

extension UIImage {
    public static func withColor(_ color: UIColor?, size: CGSize = CGSize(width: 1, height: 1)) -> UIImage? {
        let rect = CGRect(origin: CGPoint.zero, size: size)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()

        let actualColor = color ?? .clear
        context?.setFillColor(actualColor.cgColor)
        context?.fill(rect)

        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return image
    }
}

然而,上面的方法为我(在视网膜设备上)提供了 1pt,2px 边框,而默认的浅灰色边框实际上 0.5pt, 1px (又名“发际线”)。

有没有办法为UINavigationBar获得0.5磅(1px)的自定义底部边框(阴影)?

我想我可以使用运行时生成的背景图像,大部分是实心的,但是我的选择颜色的1px边框在底部“烘焙”。但这看起来似乎不够优雅,我不确定当导航栏高度发生变化时它是如何工作的:图像是切片,还是只是拉伸,或者是什么?

2 个答案:

答案 0 :(得分:1)

基于此处找到的所选答案(因为它已经很小而变化很小):

How to change the border color below the navigation bar?

// in viewDidLoad
UIView * navBorder = [[UIView alloc] initWithFrame:CGRectMake(0,
                                                              self.navigationController.navigationBar.frame.size.height, // <-- same height, not - 1
                                                              self.navigationController.navigationBar.frame.size.width,
                                                              1/[UIScreen mainScreen].scale)]; // <-- 5/5S/SE/6 will be 0.5, 6+/X will be 0.33
// custom color here
[navBorder setBackgroundColor:customColor];

[self.navigationController.navigationBar addSubview:navBorder];


信用这里以编程方式查找比例: Get device image scale (e.g. @1x, @2x and @3x)

*注意:
iPhone 6 + / X是x3,因此1px高度为0.33pt
iPhone 5 / 5S / SE / 6是x2,因此1px高度为0.5pt
在模拟器中测试,可能需要在实际设备上进行验证。

与具有自定义颜色的默认导航栏视觉相同。

答案 1 :(得分:-1)

我相信你想删除阴影。这应该有助于此。

[[UINavigationBar appearance] setShadowImage:[UIImage new]];

如果您想要使用不同颜色的阴影,那么您可以使用所需的颜色创建图像并使用它而不是

[UIImage new]

您可以使用此类内容自行生成图像

+ (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    const CGFloat alpha = CGColorGetAlpha(color.CGColor);
    const BOOL opaque = alpha == 1;
    UIGraphicsBeginImageContextWithOptions(rect.size, opaque, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}