在整个应用程序中禁用UITextField的自动更正

Hle*_*ess 6 objective-c uikit ios

我想在我的应用程序中的所有文本字段上禁用自动更正.我知道autocorrectionTypeUITextField 上的属性.但是,当我开始开发应用程序时,我没有将UITextField子类化.所以我正在寻找一种解决方案,通过一行代码在我的所有TextField上禁用自动更正.

我尝试使用[UITextField appearance],适用于键盘外观:

[[UITextField appearance] setKeyboardAppearance:UIKeyboardAppearanceDark];
Run Code Online (Sandbox Code Playgroud)

但不适用于自动更正,因为以下代码行:

[[UITextField appearance] setAutocorrectionType:UITextAutocorrectionTypeNo];
Run Code Online (Sandbox Code Playgroud)

导致不一致异常:

 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Please file a radar on UIKit if you see this assertion.'
Run Code Online (Sandbox Code Playgroud)

Mru*_*nal 1

我建议使用子类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;
}
Run Code Online (Sandbox Code Playgroud)

现在在整个项目中使用CustomTextField (无论是在代码中还是在故事板中)。

希望这可以帮助。