SWIFTUI - 即使使用 Clipped() 后,选择器视图触摸/可滚动区域仍然超出框架

Apo*_*eed 5 swiftui

SWIFTUI 我有一个选择器视图(轮式),我将其框架剪裁()到屏幕的一半宽度,从 UI 中您可以看到它的一半,但可滚动区域在该框架之外仍然可操作。如何删除该外部区域以使其不可滚动?

HStack(spacing: 0) {
    Picker(selection: self.$viewModel.selectedFrameworkIndex, label: Text("")) {
    ForEach(0 ..< viewModel.Categories.count) {
        Text(self.viewModel.Categories[$0])
            .foregroundColor((self.colorScheme == .dark) ? Color.white : Color.black)
         }
    }
    .frame(width: width / 2) // width is width of my screen
    .clipped() 
}
Run Code Online (Sandbox Code Playgroud)

Ree*_*eed 8

在 .clipped() 之后添加 .compositingGroup()

HStack(spacing: 0) {
Picker(selection: self.$viewModel.selectedFrameworkIndex, label: Text("")) {
ForEach(0 ..< viewModel.Categories.count) {
    Text(self.viewModel.Categories[$0])
        .foregroundColor((self.colorScheme == .dark) ? Color.white : Color.black)
     }
}
.frame(width: width / 2) // width is width of my screen
.clipped() 
.compositingGroup()  }    
Run Code Online (Sandbox Code Playgroud)

  • 不再适用于 iOS 15.1 (10认同)