title: How do I disable the "Space" Key on the keyboard in Swift?
body: <p>I'm creating a login system and I don't want spaces to be allowed in the username field. Instead of check and validating the field, I want to prevent spaces from being added.</p>
  
  <p>So, every time the user presses the spacebar, nothing should happen. How can I do that? 
  I've seen Instagram do this.</p>
  
  <p>This is my code so far:</p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController, UITextFieldDelegate {
  
      @IBOutlet weak var signUp: UIBarButtonItem!
      @IBOutlet weak var navBar: UINavigationBar!
      @IBOutlet weak var UsernameText: UITextField!
  
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
               self.navigationController?.navigationBar.clipsToBounds = true
          UsernameText.attributedPlaceholder = NSAttributedString(string:"TypeYourUsername",
              attributes:[NSForegroundColorAttributeName: UIColor.whiteColor()])
  
          func textField(UsernameText: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -&gt; Bool {
              if (string == " ") {
                  return false
              }
              return true
          }
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      override func viewWillAppear(animated: Bool) {
          UsernameText.becomeFirstResponder()
      }
  
  
      override func prefersStatusBarHidden() -&gt; Bool {
          return true
      }
  }
  </code></pre>