【问题标题】:SwiftUI 2.0: How to customize the iOS 14 navigation bar with toolbar() without a title?SwiftUI 2.0:如何使用没有标题的工具栏()自定义 iOS 14 导航栏?
【发布时间】:2021-02-16 07:00:00
【问题描述】:

我的 iOS 14 应用屏幕有以下设置:

import SwiftUI
import UIKit

struct MyNavigationView: View {
    
    init() { // Color Settings
        UINavigationBar.appearance().barTintColor = UIColor(named:"ToolbarBackgroundColor")
        UINavigationBar.appearance().tintColor = UIColor(named: "ToolbarForegroundColor")
        UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor(named:"ToolbarForegroundColor")]
        UIToolbar.appearance().barTintColor = UIColor(named:"ToolbarBackgroundColor")
        UIToolbar.appearance().tintColor = UIColor(named: "ToolbarForegroundColor")
       }
       
    var body: some View {
        NavigationView {
            Text("Hello, SwiftUI 2.0 World!") // content
                .navigationBarTitleDisplayMode(.inline)
                .navigationTitle("MyApp") // navigation bar title
                .toolbar(content: {
                    // navigation bar leading item
                    ToolbarItem(placement: .navigationBarLeading) {
                        Text("Hi!").foregroundColor(Color("ToolbarForegroundColor"))
                    }
                    // navigation bar trailing item
                    ToolbarItem(placement: .navigationBarTrailing) {
                        VStack(content: {
                            Text("up").foregroundColor(Color("ToolbarForegroundColor"))
                            Text("down").foregroundColor(Color("ToolbarForegroundColor"))
                        })
                    }
                    // toolbar at the bottom of the screen
                    ToolbarItemGroup(placement: .bottomBar) {
                        Button(action:{
                            print("home")
                        }) {
                            Image(systemName: "house.fill")
                                .renderingMode(.template)
                                .foregroundColor(Color("ToolbarForegroundColor"))
                        }
                        Spacer()
                        Button(action:{
                            print("pages")
                        }) {
                            Image(systemName: "square.grid.3x2")
                                .renderingMode(.template)
                                .foregroundColor(.white)
                        }
                        Spacer()
                        Button(action:{
                            print("like")
                        }) {
                            Image(systemName: "star.fill")
                                .renderingMode(.template)
                                .foregroundColor(.white)
                        }
                        Spacer()
                        Button(action:{
                            print("user")
                        }) {
                            Image(systemName: "person.fill")
                                .renderingMode(.template)
                                .foregroundColor(.white)
                        }
                    }
                })
        }
    }
}

到目前为止它仍然有效......

  • 但我对该行有多个警告 UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor(named:"ToolbarForegroundColor")] 如果我将UIColor 替换为Color 就像 UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: Color("ToolbarForegroundColor")] 警告消失了,但我遇到了崩溃……有什么问题?

  • 而且我也想去掉导航栏标题... 可以吗?

【问题讨论】:

    标签: swiftui uinavigationbar uitoolbar swiftui-navigationview


    【解决方案1】:

    我通过向导航栏标题添加默认值来解决警告:

    UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor(named:"ToolbarForegroundColor") ?? .white]
    

    仍在寻找如何摆脱标题。

    编辑:暂时用空字符串去掉标题...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-01
      • 1970-01-01
      相关资源
      最近更新 更多