简体   繁体   English

Swift 5 自定义导航栏封面标题和按钮

[英]Swift 5 custom navigation bar cover title and buttons

After seeking a lot and trying many solutions, nope fixed my problem.在寻求了很多并尝试了许多解决方案之后,没有解决我的问题。 In my app I customized the UINavigationController in order to have blur effect:在我的应用程序中,我定制了 UINavigationController 以获得模糊效果:

import UIKit

class CustomNavigationController: UINavigationController { class 自定义导航控制器:UINavigationController {

 override func viewDidLoad() { super.viewDidLoad() let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .regular)) visualEffectView.frame = (self.navigationBar.bounds.insetBy(dx: 0, dy: -40).offsetBy(dx: 0, dy: -40)) self.navigationBar.isTranslucent = true self.navigationBar.setBackgroundImage(UIImage(), for: .default) self.navigationBar.addSubview(visualEffectView) self.navigationBar.sendSubviewToBack(visualEffectView) }

}

Then in Main.storyboard I selected the customized class for the navigation controller item.然后在 Main.storyboard 我为导航 controller 项目选择了定制的 class。

The blur effect works properly, the status icons are correctly visible, but not the standard navigation bar items: left button, title and right button.模糊效果正常,状态图标正确可见,但不是标准的导航栏项目:左键、标题和右键。

For a moment they appears but soon after the customized navigation bar covers them.它们会出现片刻,但在自定义导航栏覆盖它们之后不久。

在此处输入图像描述

I'm using Xcode 12.4 and I'm running the app on iPhone Xr.我正在使用 Xcode 12.4,我正在 iPhone Xr 上运行该应用程序。

How can I show the navigation bar elements again?如何再次显示导航栏元素?

Thanks a lot in advance.提前非常感谢。

Translucent navigation bars in iOS already blur the content behind the bar, so you shouldn't need to add a UIVisualEffectView nor set a backgroundImage . iOS 中的半透明导航栏已经模糊了导航栏后面的内容,因此您不需要添加UIVisualEffectView或设置backgroundImage

If you modify your code to just:如果您将代码修改为:

override func viewDidLoad() 
{
    super.viewDidLoad()
    self.navigationBar.isTranslucent = true
}

does this not achieve the visual effect you're looking for?这没有达到你想要的视觉效果吗?

If not, please try the following adjustment to your methodology:如果没有,请尝试对您的方法进行以下调整:

override func viewDidLoad() 
{
    super.viewDidLoad()

    self.navigationBar.isTranslucent = true

    // create a UIImageView
    let backgroundImage: UIImageView = UIImageView()
    backgroundImage.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    backgroundImage.image = UIImage()

    // add a blur effect to the ImageView
    let visualEffectView: UIVisualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .regular))
    visualEffectView.frame = (self.navigationBar.bounds.insetBy(dx: 0, dy: -40).offsetBy(dx: 0, dy: -40))
    backgroundImage.addSubview (visualEffectView)

    // and set that as your backgroundImage on the navigationBar
    self.navigationBar.setBackgroundImage(backgroundImage.image, for: .default)
}

this adds the blur effect to the backgroundImage.这将模糊效果添加到背景图像。 This seems to work for me, but the visual effect is no different than just using my first suggestion, likely because backgroundImage.image == nil .这似乎对我有用,但视觉效果与仅使用我的第一个建议没有什么不同,可能是因为backgroundImage.image == nil

This is certainly an improved approach in that it doesn't add unexpected subviews into the UINavigationBar view hierarchy, and I observed both methods did not affect the visibility of the bar controls.这当然是一种改进的方法,因为它不会将意外的子视图添加到UINavigationBar视图层次结构中,而且我观察到这两种方法都不会影响栏控件的可见性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM