Rasmus Puls
Rasmus Puls

Reputation: 3229

iOS (swift) exit/dismiss preselect textView in share extension

when launching the iOS share extension the textView will by default already be selected / entered. (the keyboard will be visible and the textView will be in edit mode)

I don't want this to happen, how do I programatically exit the textView

override func viewDidLoad() {
  self.textView.exit() // obviously doesn't work
}

I see tons of posts about how to exit when user press enter on the keyboard, I do not want to do it "when something delegate" I just want the textview to not be in edit mode when the extension is launched (on viewDidLoad).

I have also tried (as suggested in other post)

self.textView.endEditing(true)

which did not hide the keyboard or exit the textView

Upvotes: 0

Views: 170

Answers (1)

AamirR
AamirR

Reputation: 12218

You can call textView.resignFirstResponder() in presentationAnimationDidFinish

class ShareViewController: SLComposeServiceViewController {

    var textViewTintColor: UIColor?

    override func viewDidLoad() {
        super.viewDidLoad()

        // hide cursor which appears during presentation animation
        self.textViewTintColor = self.textView.tintColor
        self.textView.tintColor = .clear
    }

    override func presentationAnimationDidFinish() {
        super.presentationAnimationDidFinish()

        guard let tintColor = self.textViewTintColor else { return }

        self.textView.resignFirstResponder()

        // reset cursor
        self.textView.tintColor = tintColor
        self.textViewTintColor = nil
    }

}

Upvotes: 2

Related Questions