【问题标题】:SwiftUI issue on iOS 14 with Scrollview HStack content being cut offiOS 14 上的 SwiftUI 问题,Scrollview HStack 内容被切断
【发布时间】:2021-01-23 07:45:24
【问题描述】:

我有一个使用 SwiftUI 在 iOS 13 上完美显示的按钮列表,但在 iOS 14 上,它会在屏幕结束的地方切断内容。

关于 HStacks 如何渲染屏幕上没有的内容有什么变化吗?我曾经滚动并能够看到所有按钮。

我会附上一些截图和代码。

var body: some View {
        VStack(alignment: .leading, spacing: 0){
            Text("Select a venue type")
                .font(.custom("MavenProBold", size: 16))
                .padding(.leading, 16)
                .padding(.top, 18)
                .foregroundColor(Color.black)
            
            ScrollView(.horizontal, showsIndicators: false) {
                HStack(alignment: .center, spacing: 4, content: {
                    
                    
                    Button(action: {
                        self.selectedButtonIndex = 0
                        
                    })
                    {
                        VStack(alignment: .center, spacing: 0, content: {
                            ZStack(alignment: .bottomTrailing){
                                
                                Image(systemName: "star.fill")
                                    .frame(width: circleFrameSize, height: circleFrameSize, alignment: .center)
                                    .font(.title)
                                    .background(Color(hexString: "#1A88FF"))
                                    .foregroundColor(Color.white)
                                    .clipShape(Circle())
                                
                            }
                            Text("Things to do")
                                    .padding(.top, 8)
                                    .font(.custom("MavenProBold", size: 12))
                                    .multilineTextAlignment(.center)
                                    .lineLimit(50)
                        })
                        .frame(width: 80, height: 80, alignment: .center)
                        .padding(.all, 10)
                        .foregroundColor(Color.black)
                        
                    }
                    
                    Button(action: {
                        self.selectedButtonIndex = 0
                        
                    })
                    {
                        VStack(alignment: .center, spacing: 0, content: {
                            ZStack(alignment: .bottomTrailing){
                                
                                Image(systemName: "star.fill")
                                    .frame(width: circleFrameSize, height: circleFrameSize, alignment: .center)
                                    .font(.title)
                                    .background(Color(hexString: "#1A88FF"))
                                    .foregroundColor(Color.white)
                                    .clipShape(Circle())
                                
                            }
                            Text("Things to do")
                                    .padding(.top, 8)
                                    .font(.custom("MavenProBold", size: 12))
                                    .multilineTextAlignment(.center)
                                    .lineLimit(50)
                        })
                        .frame(width: 80, height: 80, alignment: .center)
                        .padding(.all, 10)
                        .foregroundColor(Color.black)
                        
                    }

                    
                    Button(action: {
                        self.selectedButtonIndex = 0
                        
                    })
                    {
                        VStack(alignment: .center, spacing: 0, content: {
                            ZStack(alignment: .bottomTrailing){
                                
                                Image(systemName: "star.fill")
                                    .frame(width: circleFrameSize, height: circleFrameSize, alignment: .center)
                                    .font(.title)
                                    .background(Color(hexString: "#1A88FF"))
                                    .foregroundColor(Color.white)
                                    .clipShape(Circle())
                                
                            }
                            Text("Things to do")
                                    .padding(.top, 8)
                                    .font(.custom("MavenProBold", size: 12))
                                    .multilineTextAlignment(.center)
                                    .lineLimit(50)
                        })
                        .frame(width: 80, height: 80, alignment: .center)
                        .padding(.all, 10)
                        .foregroundColor(Color.black)
                        
                    }

                    
                    Button(action: {
                        self.selectedButtonIndex = 0
                        
                    })
                    {
                        VStack(alignment: .center, spacing: 0, content: {
                            ZStack(alignment: .bottomTrailing){
                                
                                Image(systemName: "star.fill")
                                    .frame(width: circleFrameSize, height: circleFrameSize, alignment: .center)
                                    .font(.title)
                                    .background(Color(hexString: "#1A88FF"))
                                    .foregroundColor(Color.white)
                                    .clipShape(Circle())
                                
                            }
                            Text("Things to do")
                                    .padding(.top, 8)
                                    .font(.custom("MavenProBold", size: 12))
                                    .multilineTextAlignment(.center)
                                    .lineLimit(50)
                        })
                        .frame(width: 80, height: 80, alignment: .center)
                        .padding(.all, 10)
                        .foregroundColor(Color.black)
                        
                    }

                    
                    Button(action: {
                        self.selectedButtonIndex = 0
                        
                    })
                    {
                        VStack(alignment: .center, spacing: 0, content: {
                            ZStack(alignment: .bottomTrailing){
                                
                                Image(systemName: "star.fill")
                                    .frame(width: circleFrameSize, height: circleFrameSize, alignment: .center)
                                    .font(.title)
                                    .background(Color(hexString: "#1A88FF"))
                                    .foregroundColor(Color.white)
                                    .clipShape(Circle())
                                
                            }
                            Text("Things to do")
                                    .padding(.top, 8)
                                    .font(.custom("MavenProBold", size: 12))
                                    .multilineTextAlignment(.center)
                                    .lineLimit(50)
                        })
                        .frame(width: 80, height: 80, alignment: .center)
                        .padding(.all, 10)
                        .foregroundColor(Color.black)
                        
                    }

                    
                    
                    
                    
                })
                    .padding(.leading, 8)
                    .padding(.trailing, 8)
                    .padding(.bottom, 8)
            }
        }
     
    }

【问题讨论】:

  • 提供的代码快照(带有复制的缺失部分)适用于 Xcode 12.0 / iOS 14。所以问题可能在其他一些代码中。
  • 我会在别处再看看,谢谢!我已经将 hstack 和 scrollview 设置为具有不同的背景颜色,并且 hstack 颜色在屏幕结束的地方结束,您可以看到 scrollview 背景颜色。奇怪的是,hstack 中的按钮仍然可以点击并且功能正常,它们只是没有出现,并且 hstack 的可见性,而不是功能性让位于滚动视图。
  • 我也刚刚注意到这种行为!我现在正试图弄清楚并在我们的代码中找到共同点。按钮可以工作,但在超出手机宽度时不可见。
  • 我发现了问题,我在 ScrollView 上使用了 clipShape。我在 stackoverflow.com/questions/56760335/… 上使用了带有 Mojtaba Hosseini 制作的特定角的扩展的cornerRadius,也许你也在某处剪裁形状。它确实曾经在iOS 13上工作过,但仍然不知道为什么。我已经删除了 clipShape,但不幸的是,现在我的 UI 并没有达到应有的水平。

标签: ios swift swiftui


【解决方案1】:

我也遇到过这个问题。当您在 ScrollView 之外有一个自定义的 clipShape 时,就会发生这种情况。自定义是指形状的自定义Path

根据我的测试,当您使用内置形状时,它可以正常工作,例如:

view.clipShape(Circle())

当您使用自定义形状但使用内置路径时,它也可以正常工作,例如:

view.clipShape(CustomShape())

// which CustomShape is something like:

struct CustomShape: Shape {
    func path(in rect: CGRect) -> Path {
        return Path(roundedRect: rect, cornerSize: CGSize(width: 10, height: 10)
    }
}

但是当您在 CustomShape 中使用自定义路径时,会出现此问题:

view.clipShape(CustomShape())

// which CustomShape is something like:

struct CustomShape: Shape {
    func path(in rect: CGRect) -> Path {
        let path = UIBezierPath(roundedRect: rect,
                                byRoundingCorners: corners,
                                cornerRadii: CGSize(width: radius, height: radius))
            return Path(path.cgPath)
    }
}

我也尝试过手动绘制路径(使用move、addLine、addArc),还是不行。

因此,解决方法是在自定义形状中使用内置路径。我猜这是 iOS 14 的错误,因为它在 iOS 13 上运行良好。

【讨论】:

  • 想说,在 Xcode 12(也支持 iOS 14)中没有注意到类似的问题,但从我更新到 Xcode 12.1 开始。而你的解决方法并不能解决我的问题
  • 您是否在任何地方使用 clipShape 或 clipShae(Circle())?
  • 嗨@AccidBright,很抱歉听到这个消息。我在 iOS 14.1 上使用 Xcode 12.1 测试了我的代码,在我的情况下,使用 clipShae(Circle()) 之类的东西可以正常工作。
  • 嗨@joelgate,是的,我使用clipShape 在视图的左上角和右上角创建了一个圆角。
  • 谢谢@KyleXie!这让我发疯了。我使用 .contentShape 和 .mask() 修饰符仅将可滚动表的两个顶角舍入,但我不知道为什么底部会被剪裁超过一定大小。
猜你喜欢
  • 1970-01-01
  • 2021-01-14
  • 2021-02-05
  • 2017-08-31
  • 2020-10-27
  • 1970-01-01
  • 1970-01-01
  • 2017-12-31
  • 2021-07-31
相关资源
最近更新 更多