我正在手动在SwiftUI中逐个屏幕自定义返回按钮,如何使此更改全局生效?

huangapple go评论107阅读模式
英文:

I'm currently customizing the back button in SwiftUI manually screen by screen, how do I make this change global?

问题

我的原始目标是调整返回按钮,使其不包含“返回”文本。但我的当前实现需要将以下代码添加到需要返回按钮的每个屏幕上。如何将存在于多个屏幕上的此代码变成一个更全局的更改?

  1. @Environment(\.presentationMode) var presentationMode
  2. // 任何内容
  3. .toolbar {
  4. ToolbarItem(placement: .navigationBarLeading) {
  5. Button(action: {
  6. self.presentationMode.wrappedValue.dismiss()
  7. }) {
  8. HStack {
  9. Image(systemName: "arrow.left")
  10. .foregroundColor(.black)
  11. .font(.headline)
  12. }
  13. }
  14. }
  15. }

结果:

我正在手动在SwiftUI中逐个屏幕自定义返回按钮,如何使此更改全局生效?

对于使这个更改变为全局的任何帮助将不胜感激 我正在手动在SwiftUI中逐个屏幕自定义返回按钮,如何使此更改全局生效?

英文:

My original goal was to adjust the back button so it did not include the "back" text. But my current implementation requires adding the following code to each screen that needs a back button. How can I take this code which is on multiple screens and make it a more global change?

  1. @Environment(\.presentationMode) var presentationMode
  2. // Whatever content
  3. .toolbar {
  4. ToolbarItem(placement: .navigationBarLeading) {
  5. Button(action: {
  6. self.presentationMode.wrappedValue.dismiss()
  7. }) {
  8. HStack {
  9. Image(systemName: "arrow.left")
  10. .foregroundColor(.black)
  11. .font(.headline)
  12. }
  13. }
  14. }
  15. }

The result:

我正在手动在SwiftUI中逐个屏幕自定义返回按钮,如何使此更改全局生效?

Any help on making this change global will be much appreciated 我正在手动在SwiftUI中逐个屏幕自定义返回按钮,如何使此更改全局生效?

答案1

得分: 0

感谢 @rapiddevice 指出我可以创建一个自定义修饰符,这将使事情变得容易得多。

  1. import SwiftUI
  2. struct CustomBackButton: ViewModifier {
  3. @Environment(\.presentationMode) var presentationMode
  4. func body(content: Content) -> some View {
  5. content
  6. .navigationBarBackButtonHidden(true)
  7. .toolbar {
  8. ToolbarItem(placement: .navigationBarLeading) {
  9. Button(action: {
  10. self.presentationMode.wrappedValue.dismiss()
  11. }) {
  12. HStack {
  13. Image(systemName: "arrow.left")
  14. .foregroundColor(.black)
  15. .font(.headline)
  16. }
  17. }
  18. }
  19. }
  20. }
  21. }
  22. extension View {
  23. func customBackButton() -> some View {
  24. modifier(CustomBackButton())
  25. }
  26. }
英文:

Thank you to @rapiddevice for pointing out I can make a custom modifier which makes things A LOT easier.

  1. import SwiftUI
  2. struct CustomBackButton: ViewModifier {
  3. @Environment(\.presentationMode) var presentationMode
  4. func body(content: Content) -> some View {
  5. content
  6. .navigationBarBackButtonHidden(true)
  7. .toolbar {
  8. ToolbarItem(placement: .navigationBarLeading) {
  9. Button(action: {
  10. self.presentationMode.wrappedValue.dismiss()
  11. }) {
  12. HStack {
  13. Image(systemName: "arrow.left")
  14. .foregroundColor(.black)
  15. .font(.headline)
  16. }
  17. }
  18. }
  19. }
  20. }
  21. }
  22. extension View {
  23. func customBackButton() -> some View {
  24. modifier(CustomBackButton())
  25. }
  26. }

huangapple
  • 本文由 发表于 2023年2月10日 06:54:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定