从SwiftUI中的“后退"按钮中删除文本 [英] Remove the text from back button in SwiftUI

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

问题描述

简而言之,我要

(应删除家庭)

到目前为止,我还没有找到直接访问 NavigationBarButton 的方法,并且设法找到了以下内容,这似乎是我迄今为止找到的唯一修改按钮的方法:

  struct MyList:查看{var body:some View {文字("MyList").navigationBarTitle(Text(verbatim:"MyList"),displayMode:.inline).navigationBarItems(开头:Text(<"))}} 

但是,我丢失了默认的返回图片并出现了难看的<代替.

解决方案

您需要设置后退按钮将弹出的视图的标题:

  struct ContentView:视图{var body:some View {NavigationView {VStack {NavigationLink(目的地:DetailView()){文字(推送视图")}} .navigationBarTitle(",displayMode:.inline)}}}struct DetailView:查看{var body:some View {文字(详细信息视图")}} 

或者,根据显示状态,有条件地设置或取消设置源视图的标题,您可以使用下面的代码.

请注意,isActive参数存在错误,但是很可能很快就会解决.这是对提到的错误的引用,该错误 SwiftUI:不推荐使用NavigationDestinationLink

  struct ContentView:视图{@State private var active:布尔=假var body:some View {NavigationView {VStack {NavigationLink(目的地:DetailView(),isActive:$ active){文字(推送视图")}} .navigationBarTitle(!active?查看标题":",displayMode:.inline)}}}struct DetailView:查看{var body:some View {文字(详细信息视图")}} 

In short, I want to do this, but with SwiftUI.

(Home should be removed)

So far, I have not found a way to access the NavigationBarButton directly, and have managed to find the following that appears to be the only way I can find to date for modifying the button:

struct MyList: View {
    var body: some View {
            Text("MyList")
            .navigationBarTitle(Text(verbatim: "MyList"), displayMode: .inline)
            .navigationBarItems(leading: Text("<"))
    }
}

However, I lose the default return image and get an ugly < instead.

解决方案

You need to set the title of the view that the back button will pop to:

struct ContentView: View {    
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: DetailView()) {
                    Text("push view")
                }
            }.navigationBarTitle("", displayMode: .inline)
        }
    }
}

struct DetailView: View {    
    var body: some View {
        Text("Detail View")
    }
}

Alternatively, to conditionally set or unset the title of the source view, depending on the presentation status you can use the code below.

Beware that the isActive parameter has a bug, but that will most likely be solved soon. Here's a reference to the bug mentioned SwiftUI: NavigationDestinationLink deprecated

struct ContentView: View {
    @State private var active: Bool = false

    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: DetailView(), isActive: $active) {
                    Text("push view")
                }
            }.navigationBarTitle(!active ? "View Title" : "", displayMode: .inline)
        }
    }
}

struct DetailView: View {
    var body: some View {
        Text("Detail View")
    }
}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆