Josh
Josh

Reputation: 6373

UITextView does not update its height after trying to change its height programmatically

I just want to modify the height of a UITextView. The log resulting from the code below, says that in fact, the height changed from 30 to 400:

println(txtresponses.frame.height)       // returns 30    
var newFrame:CGRect=txtresponses.frame
newFrame.size.height=400
txtresponses.frame=newFrame
println(txtresponses.frame.height)   // returns 400

However, visually, the UITextView "txtresponses" remains with the same size. I am new to Swift and Xcode, so all my tricks are already exhausted here, and I dont know if it is an iOS version issue, or some typical Xcode whim. What is the correct way to modify a UITextView ´s height?

Upvotes: 0

Views: 1272

Answers (3)

Yalamandarao
Yalamandarao

Reputation: 3862

Might be issue Autolayout. u just remove the autolayout and check it will work. Check below code i hope it will help you.

Example :

  import UIKit

    class ViewController: UIViewController {

        @IBOutlet var textView: UITextView!
        @IBOutlet var butt: UIButton!

        override func viewDidLoad() {
            super.viewDidLoad()
            textView.backgroundColor = UIColor.lightGrayColor()
            // Do any additional setup after loading the view, typically from a nib.
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

        @IBAction func buttonAction(sender: UIButton)    {

            var newFrame:CGRect=textView.frame
            newFrame.size.height=400
            textView.frame=newFrame
        }


    }

Screen 1: enter image description here

Screen 2: enter image description here

Upvotes: -1

Santu C
Santu C

Reputation: 2654

Its not work because I think you are using Autolayout with constraint. Please check below url which may help you - Change height constraint programmatically

Upvotes: 0

Quentin Hayot
Quentin Hayot

Reputation: 7876

Make sure to call txtresponses.frame=newFrame in the main thread.

dispatch_async(dispatch_get_main_queue()) {
      txtresponses.frame=newFrame
    }

All UI updates must be done from the main thread.

Upvotes: 2

Related Questions