-1

so I am making a textfield programmatically. When I hit the done key in the keyboard the curser goes to the next line and key board does not disappear. I have included all the lines of codes recommened by people to make the keyboard disappear but it didn't work. can someone look at my code and advice me what I need to add? I want to hit the done key and keyboard disappear and curser doesn't go to the next line.

import UIKit

class FirstPage: UIViewController {

    let emailText: UITextView = {
        let textView = UITextView()
        let attributedText = NSMutableAttributedString(string: "Email", attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20),NSAttributedString.Key.foregroundColor: UIColor.gray])
        textView.backgroundColor = UIColor.clear
        textView.attributedText = attributedText
        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.textAlignment = .center
        textView.returnKeyType = .done
        textView.resignFirstResponder()
        textView.isScrollEnabled = false
        return textView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        emailText.delegate = self as? UITextViewDelegate
        view.addSubview(emailText)
        emailText.translatesAutoresizingMaskIntoConstraints = false
        emailText.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
    }

}
Robert Dresler
  • 10,580
  • 2
  • 22
  • 40
Sara
  • 33
  • 1
  • 5
  • Make sure you conform to UITextViewDelegate from your viewcontroller class decleration and then update the delegate setting in your viewDidLoad as: emailText.delegate = self, then Check this: https://stackoverflow.com/questions/26600359/dismiss-keyboard-with-a-uitextview – emrepun Jan 06 '19 at 15:44
  • I added the two functions to my code (func textView) but it did not work :( – Sara Jan 06 '19 at 15:48
  • I have seen all those question and I added all the lines of codes they recommended but I still can't make it work – Sara Jan 06 '19 at 15:48
  • @Sara did you add delegate method to your code? I can't see any – Robert Dresler Jan 06 '19 at 15:51
  • I have in another code. in this one I forgot. I just added it and it still doesn't work :( – Sara Jan 06 '19 at 15:53
  • can you copy paste my code into your Xcode and see how it acts in the simultaor? – Sara Jan 06 '19 at 15:54
  • It makes no sense to use a text view if you can only enter one line of text. Use a UITextField if you only want one line of text. – rmaddy Jan 06 '19 at 16:07
  • @rmaddy that's right, I didn't pay attention on text which he's setting. – Robert Dresler Jan 06 '19 at 16:09
  • Why did you delete the answer? it worked! I was trying to mark it as answered! – Sara Jan 06 '19 at 16:11
  • so you guys are saying I should not use uitextview? – Sara Jan 06 '19 at 16:12
  • Correct. A `UITextView` is for entering multiple lines of text. A `UITextField` is for when you only want a single line of text. – rmaddy Jan 06 '19 at 16:21
  • “so I am making a textfield” No you’re not. You are making a text view, a very different thing. – matt Jan 06 '19 at 16:34

1 Answers1

0

Regarding to your comments, I believe that you have delegate method for dismissing keyboard.

First problem which I see is that you view controller doesn't implement text view's delegate protocol, change it: Class: Protocol

extension FirstPage: UITextViewDelegate {
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if text == "\n" {
            textView.resignFirstResponder()
            return false
        }
        return true
    }
}

then set delegate of your text view just as self

emailText.delegate = self

Also make your text view variable lazy (also you can assign its delegate inside lazy variable initialization)

lazy var emailText: UITextView = {
    let textView = UITextView()
    ...
    textView.delegate = self
    return textView
}()

Now, let's think about content. You have UITextView for inserting email. Since for inserting email is one line of text enough, use UITextField instead of your text view.

Then you can use text field's textFieldShouldReturn delegate method for dismissing keyboard after pressing return key

func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}
Robert Dresler
  • 10,580
  • 2
  • 22
  • 40
  • is it ok if you paste the entire code here for me? I get an error using the extention – Sara Jan 06 '19 at 16:01
  • 1. In extension of 'FirstPage'. Expected declaration – Sara Jan 06 '19 at 16:02
  • @Sara where are you adding this extension? You have to add it on the same level as view controller – Robert Dresler Jan 06 '19 at 16:04
  • I am adding it at the end when I close the bracket of my class ( after the bracket) – Sara Jan 06 '19 at 16:05
  • You don't want to do this. Use a `UITextField` if you only want one line of text. – rmaddy Jan 06 '19 at 16:07
  • ok Got ya! Either way your method for the text view works. But I will try to switch to textfield now! Thank you all for helping me out. I appreciate it very much!! – Sara Jan 06 '19 at 16:19