中的导航栏中删除后退按钮文本

中的导航栏中删除后退按钮文本

本文介绍了从 SwiftUI 中的导航栏中删除后退按钮文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用 SwiftUI,得出的结论是使用导航还不是很好.我想要达到的目标如下.我终于设法摆脱了半透明背景而不会导致应用程序崩溃,但现在我遇到了下一个问题.如何摆脱 navbaritem 中的后退"文本?

I've recently started working in SwiftUI, came to the conclusion that working with navigation isn't really great yet. What I'm trying to achieve is the following. I finally managed to get rid of the translucent background without making the application crash, but now I ran into the next issue. How can I get rid of the "back" text inside the navbaritem?

我通过像这样在 SceneDelegate.swift 文件中设置默认外观来实现上述视图.

I achieved the view above by setting the default appearance in the SceneDelegate.swift file like this.

let newNavAppearance = UINavigationBarAppearance()
newNavAppearance.configureWithTransparentBackground()
newNavAppearance.setBackIndicatorImage(UIImage(named: "backButton"), transitionMaskImage: UIImage(named: "backButton"))
newNavAppearance.titleTextAttributes = [
    .font: UIFont(name: GTWalsheim.bold.name, size: 18)!,
    .backgroundColor: UIColor.white

]

UINavigationBar.appearance().standardAppearance = newNavAppearance

实现此目的的一种可能方法是覆盖导航栏项目,但这有一个缺点(SwiftUI Custom Back Button Text for NavigationView) 正如这个问题的创建者已经说过的那样,在您覆盖导航栏项目后,后退手势将停止工作.有了这个,我也想知道如何设置后退按钮的前景颜色.它现在具有默认的蓝色,但是我想用另一种颜色覆盖它.

One possible way that I could achieve this is by overriding the navigation bar items, however this has one downside (SwiftUI Custom Back Button Text for NavigationView) as the creator of this issue already said, the back gesture stops working after you override the navigation bar items. With that I'm also wondering how I could set the foregroundColor of the back button. It now has the default blue color, however I'd like to overwrite this with another color.

推荐答案

所以我实际上最终得到了以下实际可行的解决方案.我正在像这样覆盖导航栏项目

So I actually ended up with the following solution that actually works. I am overwriting the navigation bar items like so

.navigationBarItems(leading:
    Image("backButton")
        .foregroundColor(.blue)
        .onTapGesture {
            self.presentationMode.wrappedValue.dismiss()
    }
)

唯一的问题是后退手势不起作用,因此通过实际扩展 UINavigationController

The only issue with this was that the back gesture wasn't working so that was solved by actually extending the UINavigationController

extension UINavigationController: UIGestureRecognizerDelegate {
    override open func viewDidLoad() {
        super.viewDidLoad()
        interactivePopGestureRecognizer?.delegate = self
    }

    public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return viewControllers.count > 1
    }
}

现在它看起来正是我想要的方式,解决方案有点老套......但它现在可以工作,希望 SwiftUI 会成熟一点,这样可以更容易地完成.

Now it's looking exactly the way I want it, the solution is kinda hacky... but it works for now, hopefully SwiftUI will mature a little bit so this can be done easier.

这篇关于从 SwiftUI 中的导航栏中删除后退按钮文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 03:34