ios – 处理iPhone X中的导航栏背景渐变

ios – 处理iPhone X中的导航栏背景渐变,第1张

概述到目前为止,我通过以下方式处理导航栏中的渐变_ let gradient = CAGradientLayer() let sizeLength = UIScreen.main.bounds.size.height * 2 let defaultNavigationBarFrame = CGRect(x: 0, y: 0, width: sizeLength, height: 64) 到目前为止,我通过以下方式处理导航栏中的渐变_

let gradIEnt = CAGradIEntLayer()    let sizeLength = UIScreen.main.bounds.size.height * 2    let defaultNavigationbarFrame = CGRect(x: 0,y: 0,wIDth: sizeLength,height: 64)    gradIEnt.frame = defaultNavigationbarFrame    gradIEnt.colors = [UIcolor(hex:"92CF1F").cgcolor,UIcolor(hex:"79AB1B").cgcolor]    UINavigationbar.appearance().setBackgroundImage(self.image(fromLayer: gradIEnt),for: .default)    UINavigationbar.appearance().tintcolor = UIcolor.white    UINavigationbar.appearance().isTranslucent = false    UINavigationbar.appearance().clipsToBounds = false    if DeviceType.IS_IPAD{        UINavigationbar.appearance().TitleTextAttributes = [NSFontAttributename : UIFont .systemFont(ofSize: 24,weight: UIFontWeightlight),NSForegroundcolorAttributename: UIcolor.white]    }    else    {        UINavigationbar.appearance().TitleTextAttributes = [NSFontAttributename : UIFont .systemFont(ofSize: 20,NSForegroundcolorAttributename: UIcolor.white]    }    UISearchbar.appearance().backgroundcolor = UIcolor.clear

但是现在在iPhone X中我有问题,因为“64”作为渐变的导航栏高度如下

请建议一个可以在每种情况下动态使用的修复程序.

解决方法 使用Alisson Barauna对你的问题的回答,我设法通过更新我的UINavigationbar扩展来解决这个问题,如下所示:

extension UINavigationbar {@objc func setGradIEntBackground(colors: [UIcolor]) {    var updatedFrame = bounds    if UIDevice().userInterfaceIdiom == .phone {        if UIScreen.main.nativeBounds.height == 2436{            updatedFrame.size.height += 44        } else {            updatedFrame.size.height += 20        }    }    let gradIEntLayer = CAGradIEntLayer(frame: updatedFrame,colors: colors)    setBackgroundImage(gradIEntLayer.createGradIEntimage(),for: UIbarMetrics.default)}

通过这样做,如果程序检测到正在使用iPhone X(屏幕高度为2436),则高度将设置为较大的iPhone X尺寸(44).

总结

以上是内存溢出为你收集整理的ios – 处理iPhone X中的导航栏背景渐变全部内容,希望文章能够帮你解决ios – 处理iPhone X中的导航栏背景渐变所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1067854.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存