0

I am new in iOS.

My task is to send feedback data from feedback screen.

In Feedback screen I have 3 fields:

  • Name textfield
  • Email textfield
  • Feedback textview

which were presented in a UIView.

Based on the content in feedback textview, the height of textview should increase as well as when ever the textview height increasing or decreasing, the UIView height also vary.

The main thing is, the textview should increase double of it's height and after that scroll action should perform.

I am using Auto-layouts to achieve this.If any one can helps me to achieve this would be great.Thank in advance.

Code ref:

import UIKit

class FeedbackViewController: UIViewController {

    @IBOutlet weak var feedbacktextview: UITextView!

    @IBOutlet weak var textviewheight: NSLayoutConstraint!


  @IBOutlet weak var containerviewheight: NSLayoutConstraint!
    override func viewDidLoad() {
        super.viewDidLoad()
 self.navigationController?.setNavigationBarHidden(true, animated: false)

    }

}
extension FeedbackViewController: UITextViewDelegate{
    override func didChangeValue(forKey key: String) {
        textviewheight.constant = feedbacktextview.contentSize.height
        containerviewheight.constant = feedbacktextview.frame.size.height
//        let fixedWidth = feedbacktextview.frame.size.width
//        let newSize = feedbacktextview.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
//        feedbacktextview.frame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)

    }
}
CodeChanger
  • 7,953
  • 5
  • 49
  • 80
Navya
  • 39
  • 7
  • Possible duplicate of [How do I size a UITextView to its content?](https://stackoverflow.com/questions/50467/how-do-i-size-a-uitextview-to-its-content) – dahiya_boy Mar 25 '19 at 09:17

3 Answers3

1

Create Outlet to the textView and Make sure you select scrollEnabled to no

textview.scrollEnabled = false

let fixedWidth = textView.frame.size.width
let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
textView.frame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
0

Try this:

First create an @IBOutlet for the UITextView and the height constraint. And then after dynamically change the height in code:

class ViewControler: UIViewController, UITextViewDelegate {

    @IBOutlet weak var txtView: UITextView!
    @IBOutlet weak var txtViewHeight: NSLayoutConstraint!


    override func viewDidLoad() {
        super.viewDidLoad()
        self.txtView.delegate = self
    }

    func textViewDidChange(textView: UITextView) {
        let sizeToFitIn = CGSizeMake(self.txtView.bounds.size.width, CGFloat(MAXFLOAT))
        let newSize = self.txtView.sizeThatFits(sizeToFitIn)
        self.txtViewHeight.constant = newSize.height
    }

}
Nick
  • 875
  • 6
  • 20
0

Try this

@IBOutlet weak var txtView: UITextView!
@IBOutlet weak var constLocationHeight: NSLayoutConstraint!

func textViewDidChange(_ textView: UITextView) {
     adjustFrames(textView, constLocationHeight)        
}

func adjustFrames(_ textView: UITextView, _ constTextview: NSLayoutConstraint)  {
        if textView.contentSize.height >= 80 {
            constTextview.constant = 80
            return;
        } else if textView.contentSize.height < 30 {
            constTextview.constant = 33
            return
        }
        constTextview.constant = textView.contentSize.height
        textView.beginFloatingCursor(at: CGPoint.zero)
        textView.endFloatingCursor()
}
Parth Patel
  • 915
  • 11
  • 33
  • My requirement is "textview height should increase upto double of it's initial height based on content as well as view should increase based on textview, if the content is crossed this textview height which is doubled, then scroll should come " – Navya Mar 26 '19 at 06:56
  • @Navya Then do this (textView.contentSize.height * 2). This will provide you double height. – Parth Patel Mar 27 '19 at 05:42
  • @Navya Please remove the IF..ELSE condition from the adjustFrames() method. – Parth Patel Mar 27 '19 at 05:45