Reputation: 291
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
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
Reputation: 1327
swift version of my Obj-C Answer
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return false
}
Upvotes: 0
Reputation: 1327
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return NO;
}
You can use this too
Upvotes: 12
Reputation: 639
Accepted answer in Swift 4:
textField.isUserInteractionEnabled = false
Upvotes: 1
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
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