You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用EqualIconWidthDomain遇类型转换错误,求助排查原因

SwiftUI标签垂直对齐代码出现类型转换错误求助

原帖专门@rob mayoff,因账号声望不足无法直接在您的回答下评论,特此发帖求助,也欢迎其他开发者帮忙解答。

在您关于《SwiftUI多标签垂直对齐》的回答基础上,我仅根据Xcode的版本兼容性警告修改了iOS最低版本要求,其余代码完全未改动,却触发了如下类型转换错误:

Cannot convert value of type 'VStack<TupleView<(Text, Divider, HStack<TupleView<(VStack<TupleView<(Label<Text, Text>, Label<Text, Image>)>>, VStack<TupleView<(Label<Text, Image>, Label<Text, Image>)>>)>>)>>' to closure result type 'Content'

想请教下这个问题是Xcode 13.4.1的版本bug导致的,还是代码存在其他隐藏问题?目前没看到其他开发者反馈过类似情况。

以下是我的完整代码结构:

fileprivate struct IconWidthKey: PreferenceKey {
    static var defaultValue: CGFloat? { nil }

    static func reduce(value: inout CGFloat?, nextValue: () -> CGFloat?) {
        switch (value, nextValue()) {
        case (nil, let next): value = next
        case (_, nil): break
        case (.some(let current), .some(let next)): value = max(current, next)
        }
    }
}

extension IconWidthKey: EnvironmentKey { }

extension EnvironmentValues {
    fileprivate var iconWidth: CGFloat? {
        get { self[IconWidthKey.self] }
        set { self[IconWidthKey.self] = newValue }
    }
}

fileprivate struct IconWidthModifier: ViewModifier {
    @Environment(\.iconWidth) var width

    func body(content: Content) -> some View {
        content
            .background(GeometryReader { proxy in
                Color.clear
                    .preference(key: IconWidthKey.self, value: proxy.size.width)
            })
            .frame(width: width)
    }
}

struct EqualIconWidthLabelStyle: LabelStyle {
    @available(iOS 14.0, *)
    func makeBody(configuration: Configuration) -> some View {
        HStack {
            configuration.icon.modifier(IconWidthModifier())
            configuration.title
        }
    }
}

@available(iOS 14.0, *)
struct EqualIconWidthDomain<Content: View>: View {
    let content: Content
    @State var iconWidth: CGFloat? = nil

    init(@ViewBuilder _ content: () -> Content) {
        self.content = content()
    }

    
    var body: some View {
        content
            .environment(\.iconWidth, iconWidth)
            .onPreferenceChange(IconWidthKey.self) { self.iconWidth = $0 }
            .labelStyle(EqualIconWidthLabelStyle())
}



struct WelcomeScreen: View{
...
@ViewBuilder
func playWelcomeScreen() -> some View{
...
EqualIconWidthDomain {
            VStack {
                Text("Le Menu")
                    .font(.caption)
                Divider()
                HStack {
                    VStack(alignment: .leading) {
                        Label(
                            title: { Text("Strawberry") },
                            icon: { Text("🍓") })
                        Label("Money", systemImage: "banknote")
                    }
                    VStack(alignment: .leading) {
                        Label("People", systemImage: "person.3")
                        Label("Star", systemImage: "star")
                    }
                }
            }
        }
...
   }
}

内容的提问来源于stack exchange,提问作者aSquaredRush

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 15:57:49