SwiftUI:可重用跨平台(iOS& macOS)视图中的导航栏标题 [英] SwiftUI: Navigation Bar Title in Reusable Cross-Platform (iOS & macOS) View

查看:360
本文介绍了SwiftUI:可重用跨平台(iOS& macOS)视图中的导航栏标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为框架创建可重用的SwiftUI View,然后可在iOS/iPadOS和macOS上使用它.

I'm attempting to create reusable SwiftUI View for a framework, which can then be used across both iOS/iPadOS and macOS.

这通常可以正常工作;但是,由于macOS视图没有导航栏,因此当视图包含在macOS目标中时,包括导航栏标题(对于iOS而言很重要)会导致错误:

This generally works fine; however, because macOS views don't have navigation bars, including a navigation bar title (important for iOS) causes an error when the view's included in a macOS target:

.navigationBarTitle(Text("Page Title"))

类型'...'的值没有成员'navigationBarTitle'

Value of type '...' has no member 'navigationBarTitle'

关于条件编译(或任何其他)方法的任何建议,都可以为两个平台构建相同的视图?

Any suggestions on a conditional compilation (or any other) approach that can allow the same view to build for both platforms?

我来的最接近的是以下.包括额外的Text视图很容易,但是由于栏标题是修饰符,因此在条件编译中仅包装该部分会产生其他错误:

The closest I've come is the following. Including the extra Text view is easy enough, but because the bar title's a modifier, wrapping just that part in conditional compilation produces other errors:

public struct ExampleView: View {

    private let pageTitle = "Page Title"

    public var body: some View {

        VStack(alignment: .center) {

            #if os(macOS)
            Text(pageTitle)
                .font(.title)
            #endif

            Spacer()           
            Text("This is the view content")
            Spacer()
        }

        #if !os(macOS)
        .navigationBarTitle(Text(pageTitle))
        #endif
    }
}

意外的平台状况(预期的"os","arch"或"swift")

Unexpected platform condition (expected 'os', 'arch', or 'swift')

函数声明了一个不透明的返回类型,但是在其主体中没有用于从其推断基础类型的返回语句

Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type

推荐答案

我建立了一种方法:将主要内容移至另一个View属性,然后对其进行修改以添加导航栏标题(如果需要),然后将其返回为body.当以这种方式分开时,可以使用条件编译.

I established one approach: Move the main content to another View property, then modify it to add the navbar title (if needed) before returning it as body. When it's separated this way, conditional compilation can be used.

这有点不雅致,但可以.它也可以用来设置特定于macOS的内容,例如视图的整体框架大小.欢迎提出更好的方法的建议!

It's a bit inelegant, but it works. It can also be used to set macOS-specific things, such as the view's overall frame size. Suggestions on a better approach are welcome!

Swift v5.1

public struct ExampleView: View {

    private let pageTitle = "Page Title"

    #if !os(macOS)
    public var body: some View {
        main.navigationBarTitle(Text(pageTitle))
    }
    #else
    public var body: some View {
        main.frame(
            minWidth: 500,
            minHeight: 500
        )
    }
    #endif
    public var main: some View {

        VStack(alignment: .center) {

            #if os(macOS)
            Text(pageTitle)
                .font(.title)
            #endif

            Spacer()           
            Text("This is the view content")
            Spacer()
        }
    }
}

这篇关于SwiftUI:可重用跨平台(iOS& macOS)视图中的导航栏标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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