Anand -
Anand -

Reputation: 291

Setting UITextField not editable or editable

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;
}

Upvotes: 21

Views: 14735

Answers (7)

Sumit Sharma
Sumit Sharma

Reputation: 443

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

textField.userInteractionEnabled = NO;

In Swift 5

textField.isUserInteractionEnabled = false

Upvotes: 20

vijeesh
vijeesh

Reputation: 1327

swift version of my Obj-C Answer

   func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        return false
    }

Upvotes: 0

vijeesh
vijeesh

Reputation: 1327

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
  return NO;
} 

You can use this too

Upvotes: 12

Matt Stobbs
Matt Stobbs

Reputation: 639

Accepted answer in Swift 4:

textField.isUserInteractionEnabled = false

Upvotes: 1

Tanuj
Tanuj

Reputation: 531

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;

Upvotes: -2

Jatin Patel - JP
Jatin Patel - JP

Reputation: 3733

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.

Upvotes: 0

vivekDas
vivekDas

Reputation: 1288

Use the below property.

textField.userInteractionEnabled = NO;

Upvotes: -1

Related Questions