【问题标题】:.Clipped() not working on Pickers SwiftUI.Clipped() 不适用于 Pickers SwiftUI
【发布时间】:2020-09-24 15:25:30
【问题描述】:

我需要创建一个包含 2 个Pickers 和 2 个DatePickers 的视图。

为了避免它们相互重叠,我使用了.clipped()。但是,这不起作用。

我可以成功调整pickers 的大小,但我无法将“选择区域”限制为它们的frame 大小。

这是我的代码:

VStack(spacing: 10) {
        Picker(selection: self.$dayTypeSelectedIndex, label: Text("")) {
            ForEach(0 ..< self.dayTypes.count, id: \.self) {
                Text(self.dayTypes[$0])
                    .foregroundColor(Color.black)
            }
        }
        .pickerStyle(SegmentedPickerStyle())
        .labelsHidden()
        .clipped()
        
        Spacer()
        
        Group {
            DatePicker("", selection: self.$dateIntervalStart, displayedComponents: .hourAndMinute)
                .labelsHidden()
                .colorInvert()
                .colorMultiply(Color.black)
                .frame(maxHeight: 50)
                .clipped()
            
            Spacer()
            
            Text("às")
                .foregroundColor(Color.black)
            
            Spacer()
            
            DatePicker("", selection: self.$dateIntervalEnd, displayedComponents: .hourAndMinute)
                .labelsHidden()
                .colorInvert()
                .colorMultiply(Color.black)
                .frame(maxHeight: 50)
                .clipped()
        }
        
        Spacer()
        
        HStack {
            Picker(selection: self.$tripNotificationDelta, label: Text("")) {
                ForEach(0...60, id: \.self) {
                    Text($0 < 10 ? "0\($0)" : "\($0)")
                        .foregroundColor(Color.black)
                }
            }
            .labelsHidden()
            .frame(maxWidth: 50, maxHeight: 50)
            .clipped()

            Text("mins before trip")
                .foregroundColor(Color.black)
        }
    }
}

我做错了什么?

我使用的是 XCode 12。另外,这个界面是为不运行 iOS 14 的设备构建的。

谢谢!

【问题讨论】:

    标签: ios xcode datepicker swiftui picker


    【解决方案1】:

    所以,我想通了。只需将.compositingGroup() 放在.clipped() 之前。像这样的:

                DatePicker("", selection: self.$dateIntervalEnd, displayedComponents: .hourAndMinute)
                .labelsHidden()
                .colorInvert()
                .colorMultiply(Color.black)
                .frame(maxHeight: 50)
                .compositingGroup()
                .clipped()
    

    【讨论】:

    • 此修复也适用于 iOS 14 中的图像裁剪
    • 伟大的发现。谢谢!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-09
    相关资源
    最近更新 更多