【发布时间】:2019-02-22 00:43:38
【问题描述】:
我想将我的导航栏更改为以虚线作为边框,如下所示:
我在这个帖子中找到了一个关于如何更改导航栏边框颜色的精彩答案:Change navigation bar bottom border color Swift 特别是:https://stackoverflow.com/a/46224261/436014
通过 UIColor 扩展:
extension UIColor {
/// Converts this `UIColor` instance to a 1x1 `UIImage` instance and returns it.
///
/// - Returns: `self` as a 1x1 `UIImage`.
func as1ptImage() -> UIImage {
UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
setFill()
UIGraphicsGetCurrentContext()?.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
let image = UIGraphicsGetImageFromCurrentImageContext() ?? UIImage()
UIGraphicsEndImageContext()
return image
}
}
我想知道是否有任何方法可以调整它来构建一条虚线。我在想它可以通过绘制交替颜色的填充来以某种方式完成,但我很困惑,因为这都是单个 UIcolor 的扩展
navigationController.navigationBar.shadowImage = UIColor.black.as1ptImage()
因此,类似以下的内容显然行不通:
UIGraphicsBeginImageContext(CGSize(width: 1, height: 4))
UIGraphicsGetCurrentContext()?.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
UIGraphicsGetCurrentContext()?.fill(CGRect(x: 1, y: 0, width: 3, height: 1))
我想知道是否有人知道如何解决这个问题
【问题讨论】: