简体   繁体   English

尝试以编程方式更改其高度后,UITextView不会更新其高度

[英]UITextView does not update its height after trying to change its height programmatically

I just want to modify the height of a UITextView . 我只想修改UITextView的高度。 The log resulting from the code below, says that in fact, the height changed from 30 to 400: 以下代码产生的日志显示,实际上,高度已从30变为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. 但是,在视觉上, UITextView “ txtresponses”保持相同的大小。 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. 我是SwiftXcode新手,所以我的所有技巧都在这里用尽了,我不知道这是iOS版本的问题还是某些典型的Xcode异想天开。 What is the correct way to modify a UITextView ´s height? 修改UITextView的高度的正确方法是什么?

Make sure to call txtresponses.frame=newFrame in the main thread. 确保在主线程中调用txtresponses.frame=newFrame

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

All UI updates must be done from the main thread. 所有UI更新都必须从主线程完成。

Its not work because I think you are using Autolayout with constraint. 它不起作用,因为我认为您正在使用带有约束的自动布局。 Please check below url which may help you - Change height constraint programmatically 请检查以下网址,这可能对您有帮助-以编程方式更改高度限制

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: 屏幕1: 在此处输入图片说明

Screen 2: 屏幕2: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM