Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable autocorrection for UITextField in whole application

I want to disable Autocorrection on all textfields in my application. I'm aware of the autocorrectionType property on UITextField. However, I didn't subclass UITextField when I started to develop my application. So I'm looking for a solution to disable autocorrect on all my TextField with a single line of code.

I tried using [UITextField appearance], which works for the keyboard appearance:

[[UITextField appearance] setKeyboardAppearance:UIKeyboardAppearanceDark];

But not for autocorrection, since the following line of code:

[[UITextField appearance] setAutocorrectionType:UITextAutocorrectionTypeNo];

Results in a inconsistency exception:

 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Please file a radar on UIKit if you see this assertion.'
like image 879
Hless Avatar asked Dec 08 '15 11:12

Hless


1 Answers

I would suggest to go with sub-classing UITextField:

@interface CustomTextField : UITextField
@end
@implementation CustomTextField
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setAutocorrectionType:UITextAutocorrectionTypeNo];

        // same way add all the generalize properties which you require for all the textfields. 
    }
return self;
}

Now use CustomTextField in all over your project (either in code or in storyboard).

Hope this helps.

like image 101
Mrunal Avatar answered Nov 04 '22 21:11

Mrunal