状态栏模糊视图(半透明)在Swift 4.2 iOS 12中

时间:2019-05-16 09:38:20

标签: ios swift uivisualeffectview uiblureffect

我试图根据背景来模糊状态栏,就像我们下拉状态栏时一样。就像看到了一样

  Client SalesPerson  Qty_CAD
0      B         Bob    10000
1      B        Dave       50
2      B        Dave       10
3      A         Bob      100
4      A        Dave       50
5      A        Dave       10
6      C         Bob      100
7      C        Dave       50

我尝试了所有样式,但确实变为半透明 甚至我尝试从情节提要中的情节提要效果秀中尝试,但没有在模拟器中尝试,甚至我的 Reduce Transparency (降低透明度)也已启用

enter image description here

我想在stausbar中实现

enter image description here

但是它只会显示一个白色的条形图

1 个答案:

答案 0 :(得分:1)

您可以通过以下代码获取statusBar视图,然后尝试添加到here之类的视觉效果中

     let statWindow = UIApplication.shared.value(forKey:"statusBarWindow") as! UIView
        let statusBar = statWindow.subviews[0] as UIView
        statusBar.backgroundColor = UIColor(red: 213 / 255.0, green: 0 / 255.0, blue: 0 / 255.0, alpha: 0.7)

OR

extension UINavigationBar {
    func installBlurEffect() {
        isTranslucent = true
        setBackgroundImage(UIImage(), for: .default)
        let statusBarHeight: CGFloat = UIApplication.shared.statusBarFrame.height
        var blurFrame = bounds
        blurFrame.size.height += statusBarHeight
        blurFrame.origin.y -= statusBarHeight
        let blurView  = UIVisualEffectView(effect: UIBlurEffect(style: .light))
        blurView.isUserInteractionEnabled = false
        blurView.frame = blurFrame
        blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        addSubview(blurView)
        blurView.layer.zPosition = -1
    }
}

用法

navigationController?.navigationBar.installBlurEffect()