-1

I have a UITextView, which automatically grows according to the user's text input.

I'd like to prevent the UITextView's bottom anchor from going under the keyboard, or I'd like to autoscroll the view itself as the user inputs text which makes the textview surpass the top of the keyboard (go under the keyboard, if you will), so that the view they are typing in can be displayed while the typing occurs. Any suggestions on how to go about this?

Should I configure a scroll view in the view controller I'm doing this in?

My current code is this:

    let textView: CustomTextView = {
           
    ///Set bio Box text view:
    let field = CustomTextView()
            
     field.textColor = .label
     field.layer.borderWidth = 1
     field.layer.cornerRadius = 10
     field.font = Fonts.createSize(14)
     field.keyboardAppearance = .default
     field.tintColor = Colors.mainBrandColor
     field.placeholderLabel.text = "Tell us about yourself..."
     field.layer.borderColor = UIColor(named: "customTopColorControl")?.cgColor
            
     field.translatesAutoresizingMaskIntoConstraints = true
     field.sizeToFit()
     field.isScrollEnabled = false
     return field
  }()

  ///Setup text view:
  func setupTextView() {
    addSubview(textView)

    textView.anchor(
      top: titleLabel.bottomAnchor,
      left: leftAnchor,
      bottom: inputAccessoryView?.topAnchor,
      right: rightAnchor,
      paddingTop: 20,
      paddingLeft: 20,
      paddingBottom: 0,
      paddingRight: 20,
      width: 0,
      height: 0)
  }

  ///Adjusts the textview to fit the size of the text within it:
  func adjustUITextViewHeight(textView: UITextView) {
     textView.translatesAutoresizingMaskIntoConstraints = true
     textView.sizeToFit()
     textView.isScrollEnabled = false
  }
  • I was saying UITextView vs UIScrollView. Such as placing the UITextView within a custom view, on top of the UIScrollView. –  Aug 08 '21 at 02:10
  • 1
    Couple notes... 1) Post code that will compile without editing. We have no idea what your `CustomTextView` / `Fonts.createSize(14)` / `Colors.mainBrandColor` / etc are doing. 2) Be more descriptive of what you're trying to do. 3) If possible, use images to clarify your goal and what you're having trouble accomplishing. – DonMag Aug 08 '21 at 14:52
  • A simple solution is to give a height-constraint to the textView. Set **isScrollEnabled** property of the textView to false. Allow it to grow until it reaches the keyboard origin(you have to check this programmatically). Once it reaches the keyboard origin, make the height fixed and set **isScrollEnabled** property to true. – Akilan C B Aug 09 '21 at 10:10
  • @AkilanCB - can you add this as an answer so I can upvote and accept it? Thanks, it worked. –  Aug 20 '21 at 20:34
  • @CV3, yeah always welcome. – Akilan C B Aug 23 '21 at 06:48

1 Answers1

0

A simple solution is to give a height-constraint to the textView. Set isScrollEnabled property of the textView to false. Allow it to grow until it reaches the keyboard origin(you have to check this programmatically). Once it reaches the keyboard origin, make the height fixed and set isScrollEnabled property to true

Akilan C B
  • 199
  • 1
  • 9