【发布时间】:2021-05-17 07:37:22
【问题描述】:
我在导航栏上添加了一个渐变层,并且还具有栏按钮项,就在 iOS 13.0 + 中,当我使用此渐变层时,我的栏按钮项将隐藏,当我滑动视图返回上一页时,我会看到它们,但是当全屏时我看不到它们,尽管我可以在旧的 iOS 版本中毫无问题地看到它们,也可以在 UI 调试中看到它们
extension UIImage {
static func getGradientImage(withColors colors: [UIColor], view: UIView) -> UIImage {
let gradient = CAGradientLayer(layer: colors)
gradient.frame = view.bounds
gradient.locations = [0.0, 1.0]
gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
return UIImage.image(from: gradient) ?? UIImage()
}
static func image(from layer: CALayer) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(layer.bounds.size,
layer.isOpaque, UIScreen.main.scale)
defer { UIGraphicsEndImageContext() }
guard let context = UIGraphicsGetCurrentContext() else {
return nil
}
layer.render(in: context)
return UIGraphicsGetImageFromCurrentImageContext()
}
}
所以这个是设置导航渐变层背景,也是在这个之后
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = .clear
appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
if let navBar = navigationController?.navigationBar {
appearance.backgroundImage = UIImage.getGradientImage(withColors: [#colorLiteral(red: 0.9258083701, green: 0.4127946496, blue: 0, alpha: 1),#colorLiteral(red: 0.7787303329, green: 0.01916297711, blue: 0.1483061314, alpha: 1)], view: navBar)
}
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance
} else {}
self.navigationBackButtonTitle(str.back)
let topTitle = UIBarButtonItem()
topTitle.title = "Top Title"
self.navigationItem.leftBarButtonItem = topTitle
那么问题可能是什么?
【问题讨论】:
-
layer.insertSublayer(gradient, at: 1)
-
我测试过也不好用
标签: ios swift layer navigationbar