iPhone: Issue disabling Auto-Cap/autocorrect on a UITextField

后端 未结 6 1296
抹茶落季
抹茶落季 2020-12-24 10:52

For some reason, even though I disable the auto-cap and auto-correct of my UITextField, it\'s still capitalizing the first letter of my input.

Here is the code:

相关标签:
6条回答
  • 2020-12-24 11:10

    Updated the correct answer for Swift 5

        textField.autocorrectionType = .no
        textField.autocapitalizationType = .none
        textField.spellCheckingType = .no
    
    0 讨论(0)
  • 2020-12-24 11:12

    You're setting autocorrectionType to FALSE as if it were a BOOL, but it actually has type UITextAutocorrectionType. So FALSE is being interpreted as UITextAutocorrectionTypeDefault, which means that autocorrection is probably enabled.

    I bet it found the name "Phil" in your address book and is autocorrecting the capitalization to match.

    0 讨论(0)
  • 2020-12-24 11:16

    If you wish to disable Auto-Cap/autocorrect on a UITextField for whole project,

    then make a class which will inherit the UITextField class and init method set the autoCorrectionType to "no".

    Class AppTextField: UITextField {
    
         override init(frame: CGRect) {
             super.init(frame: frame)
    
             //setting the autocorrection to no
              self.autocorrectionType = .no
          }
    }
    

    Then in storyboard set the cusotom class for textfield to AppTextField.

    0 讨论(0)
  • 2020-12-24 11:27

    Swift 2:

    textField.autocorrectionType = .No
    textField.autocapitalizationType = .None
    
    0 讨论(0)
  • 2020-12-24 11:31

    Objective - C

    -(void)textFieldDidBeginEditing:(UITextField *)textField
    {
        textField.autocorrectionType = FALSE; // or use  UITextAutocorrectionTypeNo
        textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    
    }
    

    Swift

    func textFieldDidBeginEditing(textfield : UITextField)
    {
        textField.autocorrectionType = .No
        textField.autocapitalizationType = .None
        textField.spellCheckingType = .No
    }
    

    Swift3

      func textFieldDidBeginEditing(_ textField : UITextField)
    {
        textField.autocorrectionType = .no
        textField.autocapitalizationType = .none
        textField.spellCheckingType = .no
    }
    
    0 讨论(0)
  • 2020-12-24 11:31
    yourTextFieldName.autocorrectionType = UITextAutocorrectionTypeNo;
    yourTextFieldName.autocapitalizationType = UITextAutocapitalizationTypeNone;
    
    0 讨论(0)
提交回复
热议问题