如何使导航栏和状态栏模糊(UIBlurEffect)?当我点击图像向下滚动(滚动视图)到其他图片时,这张图片(在这种情况下是白色机器)在导航栏下丢失了,这个图形必须在具有效果UIBlurEffect的导航栏.
没有UIBlurEffect
我试过了,但没有奏效:
func addBlurEffect() { // Add blur view let bounds = self.navigationController?.navigationBar.bounds as CGRect! let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light)) visualEffectView.frame = bounds! visualEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight] self.navigationController?.navigationBar.addSubview(visualEffectView) self.navigationController?.navigationBar.sendSubview(toBack: visualEffectView) visualEffectView.isUserInteractionEnabled = false }
首先,当滚动图片时,导航栏下方会消失.
其次,状态栏仍然是灰色的.
NavigationBar的UIBlurEffect错误,StatusBar没有UIBlurEffect
为了状态栏不保持灰色,我试着这样做,但无济于事=(
bounds.offsetBy(dx: 0.0, dy: -20.0) bounds.size.height = bounds.height + 20.0
同样在didFinishLaunchingWithOptions中的AppDelegate(他用Objective-C编写的应用程序)中,我尝试添加它,所有内容保持不变,没有任何更改:
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault]; [[UINavigationBar appearance] setShadowImage:[[UIImage alloc]init]]; [[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.0f]]; [[UINavigationBar appearance] setTranslucent:YES];
请帮忙解决问题,我搞乱了3天.
对不起,我的英语不好.
将背景图像设置为导航栏就可以了.在swift 3中,以下代码适用于我.
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light)) visualEffectView.frame = (self.navigationController?.navigationBar.bounds.insetBy(dx: 0, dy: -10).offsetBy(dx: 0, dy: -10))! self.navigationController?.navigationBar.isTranslucent = true self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) self.navigationController?.navigationBar.addSubview(visualEffectView)
输出:
@Vignesh回答这对硬代码-10是个坏主意。例如,对于iPhone X,此尺寸将无法正确设置。
// Find size for blur effect. let statusBarHeight = UIApplication.shared.statusBarFrame.size.height let bounds = self.navigationController?.navigationBar.bounds.insetBy(dx: 0, dy: -(statusBarHeight)).offsetBy(dx: 0, dy: -(statusBarHeight)) // Create blur effect. let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light)) visualEffectView.frame = bounds // Set navigation bar up. self.navigationController?.navigationBar.isTranslucent = true self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) self.navigationController?.navigationBar.addSubview(visualEffectView) self.navigationController?.navigationBar.sendSubview(toBack: visualEffectView)
我也建议您创建UINavigationController的子类,因为这是一种很好的做法。像这样:
final class func MyCustomNavigation: UINavigationController { override func viewDidLoad() { // Find size for blur effect. let statusBarHeight = UIApplication.shared.statusBarFrame.size.height let bounds = navigationBar.bounds.insetBy(dx: 0, dy: -(statusBarHeight)).offsetBy(dx: 0, dy: -(statusBarHeight)) // Create blur effect. let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light)) visualEffectView.frame = bounds // Set navigation bar up. navigationBar.isTranslucent = true navigationBar.setBackgroundImage(UIImage(), for: .default) navigationBar.addSubview(visualEffectView) navigationBar.sendSubview(toBack: visualEffectView) } }