swift uitextfield only numbers keyboard lock programmatically
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// you can set the following two properties for the text field in Interface Builder, if you'd prefer
textField.delegate = self
textField.keyboardType = .numberPad
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let invalidCharacters = CharacterSet(charactersIn: "0123456789").inverted
return string.rangeOfCharacter(from: invalidCharacters) == nil
}
// or, alternatively:
//
// func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
// return string.range(of: "^\\d*$", options: .regularExpression) != nil
// }
}
Also in Swift:
- hide bottom tab bar swift
- fnb swift code
- change selection color uitableviewcell swift
- sheet swiftui
- swift ternary statement
- Expression of type 'UIViewController?' is unused warning when pop view controller in swift 3.0
- swift update uilabel text button
- swift add horizontal line uiview programmatically
- use alamofire to login in swift 5
- set image width and height swiftui
- predicate realm bool swift
- swiftui font color
- IOS create UIAlertViewController programmatically
- print things in swift
- swift show title on navigation bar programmatically
- swift how to make a variable
- displaying button title swift stackoverflow
- get item filter count swift
- deselect all cell in collectionview
- give background color swiftui
- Split a String into an array in Swift
- swift list
- how to set return type swift
- dart capitalize first letter of each word