使用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
相关产品推荐
相关产品推荐

