SwiftUI 导航后栏按钮颜色更改

问题描述 投票:0回答:1

我需要有关使用 SwiftUI 开发的项目的帮助。
当我进入详细信息屏幕时,我可以使用下面的代码块进行自定义,但滚动功能是被动的。

       .navigationBarBackButtonHidden(false)
        
        .toolbar {
            ToolbarItemGroup(placement: .topBarTrailing) {
                Button(action: {
                //action
                }) {
                    Image(systemName:"square.and.pencil")
                        .foregroundColor(.label)
                }
            }
        }

iOS 版本 17+。我希望能够从屏幕边缘滑动以返回。我想更改系统默认后退按钮的颜色。
这样,从屏幕左侧的滑动就不会被激活。
enter image description here

swiftui navigation
1个回答
0
投票

您可以尝试这种方法,通过更改

NavigationStack
色调颜色:

NavigationStack {
    ...
}
.tintColor(.black)

或者使用

.toolbar
自定义导航外观,然后执行以下棘手的方法:

struct ContentView: some View {
    @Environment(\.dismiss) private var dismiss

    var body: some View {
        ...
        .navigationBarBackButtonHidden(true)
        .toolbar {
             ToolbarItem(placement: .topBarLeading) {
                Button {
                    dismiss() //<- handle back action manually
                } label: {
                    HStack {
                        Image(systemName: "chevron.backward")
                            .foregroundColor(.red)
                    }
                }
            }
        }
    }
}

extension UINavigationController: UIGestureRecognizerDelegate {
   override open func viewDidLoad() {
        super.viewDidLoad()
        interactivePopGestureRecognizer?.delegate = nil //<- this line do the trick
    }
}

输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.