3

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)?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Michael
  • 47
  • 1
  • 6

1 Answers1

16

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.

LordColus
  • 417
  • 1
  • 5
  • 12
  • Do you know how to change the UIPickerView to grey if UserInteractionEnabled is set to false? – Michael Jan 23 '17 at 18:07
  • 1
    Just toggle between `myPickerView.alpha = 1` (Normal) and `myPickerView.alpha = 0.5` (Grayed-out). Note that you can change the 0.5 to your desired alpha value – LordColus Jan 23 '17 at 18:10
  • Setting the **getter** `isUserInteractionEnabled` does not work. Instead you can use the property `userInteractionEnabled`. – Patrick Haaser Dec 11 '19 at 13:44
  • 1
    "*Setting the getter isUserInteractionEnabled does not work.*" It does work. The [`userInteractionEnabled`](https://developer.apple.com/documentation/uikit/uiview/1622577-userinteractionenabled?language=objc) is for the Obj-C version. For Swift, [`isUserInteractionEnabled`](https://developer.apple.com/documentation/uikit/uiview/1622577-isuserinteractionenabled) has both a getter and a setter. – Gino Mempin Aug 08 '20 at 01:58