0

我是 SwiftUI 的新手,我遇到了一种让我发疯的奇怪行为。我以下面的代码为例:

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            ScrollView {
                NavigationLink(
                    destination: NonEmojiView(),
                    label: { Text("NON-emoji view.") }
                )
                Spacer()
                NavigationLink(
                    destination: EmojiView(),
                    label: { Text("Emoji view.") }
                )
            }
            .navigationTitle("I love emojis")
        }
    }
}

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


struct EmojiView: View {
    var body: some View {
        Text("Testing emojis in navigation titles.")
        .navigationTitle("♥️")
    }
}

struct NonEmojiView: View {
    var body: some View {
        Text("Testing emojis in navigation titles.")
        .navigationTitle("Boring title")
    }
}

当我运行这个应用程序(iOS 14,XCode 12.2)并点击表情符号视图(第二个导航链接),然后点击导航栏中的“<返回”按钮,内联标题样式显示在导航栏中(image1)而不是预期的(图2)。任何线索为什么?我做错什么了吗?这是一个 SwiftUI 错误吗?唯一的区别是标题中的表情符号。

编辑:正如 Luffy 所指出的,如果 ContentView 中的 navigationTitle 也包含表情符号,则效果很好。可能和标题的高度有关?这是一个 SwiftUI 错误的更多要点。

谢谢!!

4

1 回答 1

0

If you add emoji in a ContentView's navBarTitle, it will work. If you remove it, then it won't. I think it's a SwiftUI bug.

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            ScrollView {
                NavigationLink(
                    destination: NonEmojiView(),
                    label: { Text("NON-emoji view.") }
                )
                Spacer()
                NavigationLink(
                    destination: EmojiView(),
                    label: { Text("Emoji view.") }
                )
            }
            .navigationBarTitle(Text("I ❤️ emojis"), displayMode: .large) // <~ HERE
        }
    }
}

struct EmojiView: View {
    let emoji = Image(systemName: "heart.fill")
    var body: some View {
        Text("Testing emojis in navigation titles.")
            .navigationBarTitle(Text("❤️"), displayMode: .large)
    }
}

struct NonEmojiView: View {
    var body: some View {
        Text("Testing emojis in navigation titles.")
            .navigationBarTitle(Text("Boring title"), displayMode: .large)
    }
}

于 2020-11-18T14:05:34.093 回答