76

pretty trivial question, I know. But I can not find anything online.

I need to disable the user from being able to edit the text inside of a text field. So that when the click on the text, a keyboard doesn't show up.

Any ideas?

A programmatic solution or if it is possible through storyboards would be great.

Jatin Patel - JP
  • 3,725
  • 2
  • 21
  • 43
Matt Spoon
  • 2,760
  • 5
  • 25
  • 41

9 Answers9

142

Try this:

Swift 2.0:

textField.userInteractionEnabled = false

Swift 3.0:

textField.isUserInteractionEnabled = false

Or in storyboard uncheck "User Interaction Enabled"

enter image description here

John R Perry
  • 3,916
  • 2
  • 38
  • 62
Vlad
  • 7,199
  • 2
  • 25
  • 32
61

Another solution, declare your controller as UITextFieldDelegate, implement this call-back:

@IBOutlet weak var myTextField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()

    myTextField.delegate = self
}

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    if textField == myTextField {
        return false; //do not show keyboard nor cursor
    }
    return true
}
George Filippakos
  • 16,359
  • 15
  • 81
  • 92
Duyen-Hoa
  • 15,384
  • 5
  • 35
  • 44
  • 3
    Swift 4: func textFieldShouldBeginEditing(_ textField: UITextField) – Maxim Firsoff Jan 23 '18 at 09:03
  • 8
    I like this solution the best, and here's why: I might want to disable text entry in my field, but still receive touch events for the `leftView` or `rightView` of the field. An example of this: let's say I want the text field to act as a drop down. I don't want the user to be able to enter anything he or she wants, but I want to know if they click the "drop down" arrow in my text field's `rightView`. Sure there are other solutions to drop downs, but this seems clean and allows you to use the same styling as your other text fields without needing to create a whole different class / object. – Brian Sachetta Nov 02 '18 at 19:28
33

In storyboard you have two choise:

  1. set the control's 'enable' to false.

enter image description here

  1. set the view's 'user interaction enable' false

enter image description here

The diffierence between these choise is:

the appearance of UITextfild to display in the screen.

enter image description here

  1. First is set the control's enable. You can see the backgroud color is changed.
  2. Second is set the view's 'User interaction enable'. The backgroud color is NOT changed.

Within code:

  1. textfield.enable = false
  2. textfield.userInteractionEnabled = NO

    Updated for Swift 3

    1. textField.isEnabled = false
    2. textfield.isUserInteractionEnabled = false
fandro
  • 4,833
  • 7
  • 41
  • 62
Hugo
  • 1,122
  • 11
  • 12
10

If you want to do it while keeping the user interaction on. In my case I am using (or rather misusing) isFocused

self.myField.inputView = UIView()

This way it will focus but keyboard won't show up.

jeet.chanchawat
  • 5,842
  • 5
  • 38
  • 59
3

I like to do it like old times. You just use a custom UITextField Class like this one:

//
//  ReadOnlyTextField.swift
//  MediFormulas
//
//  Created by Oscar Rodriguez on 6/21/17.
//  Copyright © 2017 Nica Code. All rights reserved.
//

import UIKit

class ReadOnlyTextField: UITextField {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

    override init(frame: CGRect) {
        super.init(frame: frame)

        // Avoid keyboard to show up
        self.inputView = UIView()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        // Avoid keyboard to show up
        self.inputView = UIView()
    }

    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        // Avoid cut and paste option show up
        if (action == #selector(self.cut(_:))) {
            return false
        } else if (action == #selector(self.paste(_:))) {
            return false
        }

        return super.canPerformAction(action, withSender: sender)
    }

}
mld.oscar
  • 438
  • 6
  • 6
2

In swift 5, I used following code to disable the textfield

override func viewDidLoad() {
   super.viewDidLoad()
   self.textfield.isEnabled = false
   //e.g
   self.design.isEnabled = false
}
Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34
Mohammad Muddasir
  • 947
  • 10
  • 21
1

Swift 4.2 / Xcode 10.1:

Just uncheck behavior Enabled in your storyboard -> attributes inspector.

FRIDDAY
  • 3,781
  • 1
  • 29
  • 43
0

you can use UILabel instead if you don't want the user to be able to modify anything in your UITextField

A programmatic solution would be to use enabled property:

yourTextField.enabled = false

A way to do it in a storyboard:

Uncheck the Enabled checkbox in the properties of your UITextField

enter image description here

Novarg
  • 7,390
  • 3
  • 38
  • 74
0

textField.isUserInteractionEnabled = false

Pankaj Kainthla
  • 2,439
  • 5
  • 31
  • 61
Huy Quang
  • 1
  • 1