Remove cursor and disable copy/paste for UITextField

This tutorial is updated for X code 8.1 & swift 3.0

When you add date picker or picker view to text field then you still see cursor blinking. This does not look good at all. Also you can also copy paste other content in that text field. This can create a bugs for the app if you paste wrong info.

  1. First lets set up the x code project as usual. Create new single view application X code project. Set project name to datepicker without cursor & save it on desktop.

new x code project

2. Go to main storyboard and select view controller. Set its size to iPhone SEset-size-to-iphone-se

3. Add textfield to view controller & set it’s width to 150

add textfield

4. Add cocoa touch class file of subclass UITextField

UITextField subclass

5. Make sure to save the file in correct Group

saving subclass

6. Open TextField swift file & change import Cocoa to import UIKit

// 6
import UIKit

class TextField: UITextField {


7. Go to main storyboard, select textfield & change its custom class to TextField

set custom class

8. Open TextField swift file & add following code

class TextField: UITextField {
    // 8
    override func caretRect(for position: UITextPosition) -> CGRect {
    override func selectionRects(for range: UITextRange) -> [Any] {
        return []
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(copy(_:)) || action == #selector(selectAll(_:)) || action == #selector(paste(_:)) {
            return false
        return super.canPerformAction(action, withSender: sender)

The code above remove cursor & disable copy/paste from UITextField

9. Go to main storyboard & connect text field as outlet to view controller swift file

text field as outlet

10. Connect textfield as action to swift file & make sure to set ”Event” to ”Editing Did Begin”

textfield as action

11. Then add following code inside textfield action.

    @IBAction func textFieldEditing(_ sender: TextField) {
        // 11
        let datePicker = UIDatePicker()
        datePicker.datePickerMode =
        sender.inputView = datePicker
        datePicker.addTarget(self, action: #selector(ViewController.datePickerValueChanged), for: UIControlEvents.valueChanged)

To learn more about how to add date picker as textfield see this tutorial.

12. Add following code below above code

    // 12
    func datePickerValueChanged(_ sender: UIDatePicker) {
        let dateformatter = DateFormatter()
        dateformatter.dateStyle = DateFormatter.Style.medium
        dateformatter.timeStyle = DateFormatter.Style.none
        dateTextField.text = dateformatter.string(from:

13. Add following code to stop typing in textfield

    // 13
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

To learn more about how to close textfield see my tutorial here.

14. Build and run app in iPhone 5s. Tap the textfield and add date to the textfield.

final datepicker textfield
Download X Code Project
Challenge: Remove cursor & disable copy/paste for picker view textfield

final pickerview textfield
Download Challenge Solution
Uber challenge: Create formatted currency in text field along with removing cursor & disabling copy/paste

final currency textfield
Download Uber Solution

Total 1 Votes

Tell us how can I improve this tutorial?

+ = Verify Human or Spambot ?