ios – 将UI导航栏设置为部分半透明的Swift xCode

ios – 将UI导航栏设置为部分半透明的Swift xCode,第1张

概述我想让我的导航栏有一种颜色但不完全半透明. 这是我的代码及其结果: UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default) UINavigationBar.appearance().shadowImage = UIImage() UINavigat 我想让我的导航栏有一种颜色但不完全半透明.
这是我的代码及其结果:

UINavigationbar.appearance().setBackgroundImage(UIImage(),forbarMetrics: UIbarMetrics.Default)    UINavigationbar.appearance().shadowImage = UIImage()    UINavigationbar.appearance().translucent = true    UINavigationbar.appearance().barTintcolor = UIcolor(red: 0,green: 107/255,blue: 178/255,Alpha: 0.5)

但是,如果我将’半透明’变为假,即使用此代码:

UINavigationbar.appearance().setBackgroundImage(UIImage(),forbarMetrics: UIbarMetrics.Default)    UINavigationbar.appearance().shadowImage = UIImage()    UINavigationbar.appearance().translucent = false    UINavigationbar.appearance().barTintcolor = UIcolor(red: 0,Alpha: 0.5)

我得到这个结果:

如何使条形图的Alpha值为0.5或“半透明”?

谢谢,任何帮助将不胜感激.

解决方法 我会为导航栏设置半透明的背景图像.

使用此代码:

UINavigationbar.appearance().setBackgroundImage(UIcolor.green.toImage()?.imageWithAlpha(Alpha: 0.5),for: .default)

使用了两个扩展:

从UIcolor创建一个UIImage:

extension UIcolor {          func toImage() -> UIImage? {            return toImageWithSize(size: CGSize(wIDth: 1,height: 1))        }        func toImageWithSize(size: CGSize) -> UIImage? {            UIGraphicsBeginImageContext(size)                if let ctx = UIGraphicsGetCurrentContext() {                    let rectangle = CGRect(x: 0,y: 0,wIDth: size.wIDth,height: size.height)                    ctx.setFillcolor(self.cgcolor)                    ctx.addRect(rectangle)                    ctx.drawPath(using: .fill)                    let colorImage = UIGraphicsGetimageFromCurrentimageContext()                    UIGraphicsEndImageContext()                    return colorImage                } else {                    return nil                }            }           }

使用指定的Alpha从UIImage创建一个新的UIImage:

extension UIImage {    func imageWithAlpha(Alpha: CGfloat) -> UIImage? {        UIGraphicsBeginImageContextWithOptions(size,false,scale)        draw(at: CGPoint.zero,blendMode: .normal,Alpha: Alpha)        let newImage = UIGraphicsGetimageFromCurrentimageContext()        UIGraphicsEndImageContext()        return newImage    }}
总结

以上是内存溢出为你收集整理的ios – 将UI导航栏设置为部分半透明的Swift xCode全部内容,希望文章能够帮你解决ios – 将UI导航栏设置为部分半透明的Swift xCode所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存