【发布时间】:2018-10-30 15:47:42
【问题描述】:
首先,我在发布这个问题之前确实搜索了这个,但如果有答案,它就会被数百万关于如何删除的问题所掩盖导航栏的默认底部边框。
我不想移除导航栏的底部边框(“阴影”)。
我正在尝试通过使用外观代理的常用方法来“主题化”我的应用程序。 我可以使用如下代码全局更改 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 pt (1px)、自定义颜色的底部边框(阴影)?
我想我可以使用运行时生成的背景图像,该图像大部分是实心的,但在底部有一个我选择的颜色“烘焙”的 1px 边框。但这似乎充其量是不优雅的,我不确定当导航栏高度发生变化时它会如何工作:图像是切片,还是简单地拉伸,还是什么?
【问题讨论】:
-
只是想知道您是否提供高度为
0.5的默认尺寸,您会得到什么? -
@nayem 我尝试指定我通过的图像的大小。将高度设置为 0.5 并没有什么不同(我仍然得到 2px - 1pt 的高度)。
标签: ios uinavigationbar uiappearance