How to get blur effect similar to Apple maps status bar?

假如想象 提交于 2021-02-07 03:42:49

问题


I'm working on a map based iOS 11 application and want to make the status bar blurred exactly like it appears in the Apple maps.

This is how it looks on the Maps app:

Currently, I'm using UIVisualEffectView to replicate this effect. Here's my code:

let blurEffect = UIBlurEffect(style: .regular)
blurredStatusBar = UIVisualEffectView(effect: blurEffect)
blurredStatusBar?.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(blurredStatusBar!)

However, whatever UIBlurEffect I use, I'm not able to replicate the effect like Maps app. Here's how it looks for different effects:

.regular

.prominent

.light

.extraLight

I just want to the blur the status bar, without adding any saturation, vibrancy or any white color tint.

There is an instance property - backgroundFilters, which, possibly could have helped me but unfortunately it is not available for layers on iOS.

One possible solution suggested here was to capture the UIView content inside an image, then blurring and showing it. However, this is not a feasible solution in my case because I've got a map underneath the status bar which can be panned. It does not makes sense to continuously capture the underneath map as an image and blur it while the map is being panned.

I tried using ILTranslucentView, which is a little subclass of UIView that provide native iOS 7+ blur (translucent) effect. This is how it appeared:

I tried a couple of different approaches like this, but to no use.


回答1:


I found the solution for the above problem. I used VisualEffectView to achieve the effect. It lets you to customise the properties like colortint, blurRadius, scale of blur of your VisualEffectView. However, this should be used with caution. It utilizes a private UIKit API to do its magic; submitting this code to the App Store adds the risk of being rejected!




回答2:


you can try Following.

yourView.backgroundColor = UIColor.clear
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)   // Or   UIBlurEffectStyle.dark    
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = viewAdvancefilter.blurredView.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
yourView.addSubview(blurEffectView)



回答3:


You could try to use UIToolbar.
It has the same blur effect as UINavigationBar and UITabBar that cannot be achieved with UIBlurEffect.



来源:https://stackoverflow.com/questions/52112621/how-to-get-blur-effect-similar-to-apple-maps-status-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!