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

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

问题描述

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

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

.navigationBarTitle(Text(页面标题"))

<块引用>

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

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

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

public struct ExampleView: View {private let pageTitle = "页面标题";公共变量主体:一些视图{VStack(对齐:.center){#if 操作系统(macOS)文本(页面标题).font(.title)#万一间隔()Text(这是视图内容")间隔()}#if !os(macOS).navigationBarTitle(文本(页面标题))#万一}}

<块引用>

意外的平台条件(预期为os"、arch"或swift")

函数声明了一个不透明的返回类型,但它的主体中没有返回语句来推断基础类型

解决方案

我建立了一种方法:将主要内容移动到另一个 View 属性,然后修改它以添加导航栏标题(如果需要)在将其作为 body 返回之前.这样分开后,就可以使用条件编译了.

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

Swift v5.1

public struct ExampleView: View {private let pageTitle = "页面标题"#if !os(macOS)公共变量主体:一些视图{main.navigationBarTitle(文本(页面标题))}#别的公共变量主体:一些视图{主机(最小宽度:500,最小高度:500)}#万一公共变量主:一些视图{VStack(对齐:.center){#if 操作系统(macOS)文本(页面标题).font(.title)#万一间隔()Text("这是视图内容")间隔()}}}

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

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"))

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?

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
    }
}

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

解决方案

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.

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天全站免登陆