21

I want to know how to disable UITextField, i.e I placed a UIButton in the frame of UITextField for design purpose.

When I tap my button in UITextField the keyboard appears, but I don't want the keyboard to be displayed!

Here is my code so far:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    return textField !=textfiled1;
    return textField !=textfiled2;
}
Unheilig
  • 16,196
  • 193
  • 68
  • 98
Anand -
  • 291
  • 2
  • 3
  • 11
  • possible duplicate of [Easy way to disable a UITextField?](http://stackoverflow.com/questions/599733/easy-way-to-disable-a-uitextfield) – Apoorv Apr 23 '15 at 11:06

7 Answers7

20

You can enable or disable user interaction on the textfield using this property:

textField.userInteractionEnabled = NO;

In Swift 5

textField.isUserInteractionEnabled = false
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
Sumit Sharma
  • 443
  • 3
  • 8
12
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
  return NO;
} 

You can use this too

vijeesh
  • 1,317
  • 1
  • 17
  • 32
1

Accepted answer in Swift 4:

textField.isUserInteractionEnabled = false
Matt Stobbs
  • 611
  • 2
  • 8
  • 21
0

I think your UIButton is not on UITextField. so bringToFromView first then your UIButton selector method called first. Because compiler check Event in hierarchy. if first control response the event then it wan't looking for next.

I have tested your scenario and its working fine at my end.

Jatin Patel - JP
  • 3,725
  • 2
  • 21
  • 43
0

swift version of my Obj-C Answer

   func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        return false
    }
vijeesh
  • 1,317
  • 1
  • 17
  • 32
-1

Use the below property.

textField.userInteractionEnabled = NO;
vivekDas
  • 1,248
  • 8
  • 12
-2

you can achieve this with the help of: If you want to disable editing you can do this textField.editable = NO;

and if you want to make it editable again you can do this
textField.editable = YES;

Akash Shinde
  • 925
  • 3
  • 14
  • 31
Tanuj
  • 531
  • 4
  • 10