Disabling automatic scrolling of UITableView when editing UITextField inside UITableViewCell

The autoscroll-behavior is located in the UITableViewController functionality. To disable the automatic scrolling I found two ways: Use instead of the UITableViewController simply a UIViewController – set the datasource and delegate on your own. Override the viewWillAppear method and don’t call [super viewWillAppear: animated] With both solution you disable not only the Autoscroll, but also … Read more

Formatting a UITextField for credit card input like (xxxx xxxx xxxx xxxx)

If you’re using Swift, go read my port of this answer for Swift 4 and use that instead. If you’re in Objective-C… Firstly, to your UITextFieldDelegate, add these instance variables… NSString *previousTextFieldContent; UITextRange *previousSelection; … and these methods: // Version 1.3 // Source and explanation: http://stackoverflow.com/a/19161529/1709587 -(void)reformatAsCardNumber:(UITextField *)textField { // In order to make the … Read more

How to add a ‘Done’ button to numpad keyboard in iOS

The much safer approach is to use a UIToolBar with Done Button as inputAccessoryView. Sample Code : UIToolbar *keyboardDoneButtonView = [[UIToolbar alloc] init]; [keyboardDoneButtonView sizeToFit]; UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@”Done” style:UIBarButtonItemStyleBordered target:self action:@selector(doneClicked:)]; [keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]]; txtField.inputAccessoryView = keyboardDoneButtonView; Your -doneClicked method should look like this : – (IBAction)doneClicked:(id)sender { NSLog(@”Done Clicked.”); [self.view … Read more

Set the maximum character length of a UITextField in Swift

Your view controller should conform to UITextFieldDelegate, like below: class MyViewController: UIViewController, UITextFieldDelegate { } Set the delegate of your textfield: myTextField.delegate = self Implement the method in your view controller: textField(_:shouldChangeCharactersInRange:replacementString:) All together: class MyViewController: UIViewController, UITextFieldDelegate // Set delegate to class @IBOutlet var mytextField: UITextField // textfield variable override func viewDidLoad() { super.viewDidLoad() … Read more

Swift – validating UITextField

Alternatively, you can use this, which is called every time a key is pressed: name1.addTarget(self, action: “textFieldDidChange:”, forControlEvents: UIControlEvents.EditingChanged) name2.addTarget(self, action: “textFieldDidChange:”, forControlEvents: UIControlEvents.EditingChanged) name3.addTarget(self, action: “textFieldDidChange:”, forControlEvents: UIControlEvents.EditingChanged) name4.addTarget(self, action: “textFieldDidChange:”, forControlEvents: UIControlEvents.EditingChanged) func textFieldDidChange(textField: UITextField) { if name1.text?.isEmpty || name2.text?.isEmpty || name3.text?.isEmpty || name4.text?.isEmpty { //Disable button } else { //Enable button } … Read more

Why Is UITextField.text An Optional?

This is a historical thing. UITextField does not make any difference between an empty string and a nil string. In Objective-C there was no need to make a difference between them because you can call methods on nil in Objective-C. Also, there was no way in Objective-C to prevent users from assigning nil to a … Read more