Search code examples
iosswiftuipickerview

How to disable UIPickerView (Swift)?


I would like to disable the UIPickerView, but setting it to "isEnabled = false" does not work. And no, I don't want to disable the view while using it, but instead not being able to scroll through the view until a certain action is done for it to be re-enabled.

The code I tried here doesn't work (it may not even be swift code): How to disable UIPickerView (Objective-C)?


Solution

  • To disable user input, use:

    myPickerView.isUserInteractionEnabled = false
    

    Then, to re-enable user input, use:

    myPickerView.isUserInteractionEnabled = true
    

    From the Apple Documentation Page on .isUserInteractionEnabled:

    When set to false, user events—such as touch and keyboard—intended for the view are ignored and removed from the event queue. When set to true, events are delivered to the view normally.