Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWIFTUI - Picker view touch/scroll-able area is still out of frame even after using clipped()

Tags:

swiftui

SWIFTUI I have a picker view(wheel type) and I clipped() its frame to half width of the screen, and from the UI you can see its half but the scrollable area is still actionable outside that frame. How can I remove that outer area to not be scrollable?

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() 
}
like image 495
Apoorva Reed Avatar asked Jun 19 '20 03:06

Apoorva Reed


People also ask

What is ScrollView in SwiftUI?

SwiftUI’s ScrollView allows us to create scrolling containers of views relatively easily, because it automatically sizes itself to fit the content we place inside it and also automatically adds extra insets to avoid the safe area. Scroll views are vertical by default, but you can control the axis by passing in .horizontal as the first parameter.

How to add Auto add to tabview in SwiftUI?

Auto add to TabView with PageTabViewStyle style. You can control its appearance by .indexViewStyle. Integrate SwiftUI views into existing apps, and embed UIKit views and controllers into SwiftUI view hierarchies. Integrate SwiftUI views into existing apps, and embed UIKit views and controllers into SwiftUI view hierarchies.

How to use UIViewController inside SwiftUI?

Use this when you want to use UIViewController inside SwiftUI. To make any UIViewController usable in SwiftUI, create a wrapper view that conforms UIViewControllerRepresentable . Detail can be found in SwiftUI tutorials

How can I control the appearance of a view in SwiftUI?

You can control its appearance by .indexViewStyle. Integrate SwiftUI views into existing apps, and embed UIKit views and controllers into SwiftUI view hierarchies. Integrate SwiftUI views into existing apps, and embed UIKit views and controllers into SwiftUI view hierarchies.


2 Answers

Add .compositingGroup() after .clipped()

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()  }    
like image 147
Reed Avatar answered Oct 23 '22 06:10

Reed


Adding this extension worked for me:

extension UIPickerView {
    open override var intrinsicContentSize: CGSize {
        return CGSize(width: UIView.noIntrinsicMetric, height: super.intrinsicContentSize.height)
    }
}
like image 1
Objective C Avatar answered Oct 23 '22 06:10

Objective C