【问题标题】:navigationBarTitle works on iOS not MacOS appsnavigationBarTitle 适用于 iOS 而不是 MacOS 应用程序
【发布时间】:2020-11-16 05:58:45
【问题描述】:

以下代码在 iOS 和 Catalyst 上编译并运行良好。但是当我在 macOS 应用程序中运行它时,它会出现错误。这是一个已知的错误还是我做错了什么。 macOS Catalina 10.15.3 版,Xcode 11.3.1 版。

import SwiftUI

struct ContentView: View {
    var body: some View {

        NavigationView {
            List {
                Text("Item #1")
                Text("Item #2")
            }
            .navigationBarTitle("Title", displayMode: .inline)
            .navigationBarItems(leading: Text("Add"))
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

这是我得到的错误,

Value of type 'List<Never, TupleView<(Text, Text)>>' has no member 'navigationBarTitle'

【问题讨论】:

    标签: macos swiftui


    【解决方案1】:

    切换到.navigationTitle("Title"),您会发现它现在可以在 macOS、Big Sur 及更高版本上运行。

    【讨论】:

    • 卡特琳娜呢? Catalina 上没有标题?
    【解决方案2】:

    标题栏在 macOS 上不可用。这是接口声明:

    @available(iOS 13.0, tvOS 13.0, watchOS 6.0, *)
    @available(OSX, unavailable)
    extension View {
    
        /// Hides the navigation bar for this view.
        ///
        /// This modifier only takes effect when this view is inside of and visible
        /// within a `NavigationView`.
        ///
        /// - Parameters:
        ///     - hidden: A Boolean value that indicates whether to hide the
        ///       navigation bar.
        @available(OSX, unavailable)
        public func navigationBarHidden(_ hidden: Bool) -> some View
    
    
        /// Configures the title in the navigation bar for this view.
        ///
        /// This modifier only takes effect when this view is inside of and visible
        /// within a `NavigationView`.
        ///
        /// - Parameters:
        ///     - title: A description of this view to display in the navigation
        ///       bar.
        @available(OSX, unavailable)
        public func navigationBarTitle(_ title: Text) -> some View
    
    
        /// Configures the title in the navigation bar for this view.
        ///
        /// This modifier only takes effect when this view is inside of and visible
        /// within a `NavigationView`.
        ///
        /// - Parameters:
        ///     - titleKey: A key to a localized description of this view to display
        ///       in the navigation bar.
        @available(OSX, unavailable)
        public func navigationBarTitle(_ titleKey: LocalizedStringKey) -> some View
    

    【讨论】:

    • 我很困惑,如果它不可用你发布的界面是什么?是一些伪代码还是类似的东西?
    • 它是从 SwiftUI 接口声明中复制过来的。您可以 CMD-CTRL-单击navigationBarTitle 并自己查看。更新了完整的相应视图扩展。
    • 谢谢 没见过语法不清楚的。今晚我回家后将使用该功能。知道他们是否有计划提供它吗?此外,我正在使用 swiftui 学习 swift,从未真正使用过 uikit appkit 范例,所以我想知道 appkit 上是否提供导航视图,或者那是 swiftui 的东西。
    • 这是iOS的东西,AppKit中没有,所以它不可用。
    • 我希望他们计划扩展它,因为他们在应该适用于所有平台的 swiftui 上提供了它。但后来他们没有实现这些功能。在 swiftui 中为 macos 应用程序做 navigationview 类型界面的推荐方法是什么。抱歉,这方面的文档真的很糟糕,因为几乎所有教程都关注 ios。
    猜你喜欢
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    相关资源
    最近更新 更多