gpt4 book ai didi

swift - 如何在最新的iOS 14中仅在swiftui中舍入 ScrollView 的顶角

转载 作者:行者123 更新时间:2023-12-04 12:29:24 32 4
gpt4 key购买 nike

我试图只圆 ScrollView 的顶角。在 .clipShape() 修饰符中提供自定义形状,用于在早期的 iO​​S 版本中正常工作,但在最新的 iOS 14 中, ScrollView 的底部内容正在消失。请提出任何解决方法
这是用于圆角特定角的自定义形状:

struct RoundedCorner: Shape {

var radius: CGFloat = .infinity
var corners: UIRectCorner = .allCorners

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

extension View {
func cornerRadius(_ radius: CGFloat, corners: UIRectCorner) -> some View {
clipShape( RoundedCorner(radius: radius, corners: corners) )
}
}
这是实现自定义圆角修饰符的示例 View 的代码:
struct SampleView: View {

@State var fullname: String
@State var email: String
@State var phone: String
@State var username: String

var body: some View {
ZStack {
Color(.red).edgesIgnoringSafeArea(.top)
VStack {
Header()
ZStack {
Color(.systemBackground).edgesIgnoringSafeArea(.bottom)
ScrollView {
VStack {
Text("Title")
.font(.largeTitle)
.foregroundColor(.primary)
.bold()
.padding()
.frame(maxWidth: .infinity, alignment: .leading)
TextField("type here...", text: $fullname)
.padding()
TextField("type here...", text: $email)
.padding()
TextField("type here...", text: $phone)
.padding()
TextField("type here...", text: $username)
.padding()
TextField("type here...", text: $fullname)
.padding()
TextField("type here...", text: $email)
.padding()
TextField("type here...", text: $phone)
.padding()
TextField("type here...", text: $username)
.padding()
Button(action: { }, label: {
Text("Submit")
.padding()
.textCase(/*@START_MENU_TOKEN@*/.uppercase/*@END_MENU_TOKEN@*/)
})
.padding()
}
}
}
.frame(height: UIScreen.screenHeight/1.5)
.cornerRadius(10, corners: [.topLeft, .topRight])
}
}
}
}

private struct Header: View {
var body: some View {
HStack(alignment: .top) {
VStack(alignment: .leading) {
Text("")
.foregroundColor(Globals.Color.background)
Spacer()
}
Spacer()
}
}
}
这是底部内容被剪掉的屏幕截图:
Sample scrollview with custom rounded corners

最佳答案

而不是应用您的 .cornerRadius ScrollView 的修饰符,将其应用到背景:

ZStack {
Color(.systemBackground).edgesIgnoringSafeArea(.bottom)
.cornerRadius(10, corners: [.topLeft, .topRight])
ScrollView {
//...

关于swift - 如何在最新的iOS 14中仅在swiftui中舍入 ScrollView 的顶角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67353269/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com