尝试以编程方式更改其高度后,UITextView 不会更新其高度
UITextView does not update its height after trying to change its height programmatically
我只想修改一个UITextView
的高度。
下面的代码产生的日志说,事实上,
高度从 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
但是,在视觉上,UITextView
"txtresponses" 保持相同的大小。
我是Swift
和Xcode
的新手,所以我的技巧在这里已经用尽了,我不知道这是iOS版本问题,还是一些典型的Xcode
心血来潮.
修改 UITextView
身高的正确方法是什么?
确保在主线程中调用txtresponses.frame=newFrame
。
dispatch_async(dispatch_get_main_queue()) {
txtresponses.frame=newFrame
}
所有 UI 更新必须从主线程完成。
它不起作用,因为我认为您正在使用带有约束的自动布局。请检查下面的 url 这可能对您有帮助 - Change height constraint programmatically
可能是自动布局问题。您只需删除自动布局并检查它是否可以工作。查看下面的代码,希望对您有所帮助。
示例:
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
}
}
屏幕 1:
屏幕 2:
我只想修改一个UITextView
的高度。
下面的代码产生的日志说,事实上,
高度从 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
但是,在视觉上,UITextView
"txtresponses" 保持相同的大小。
我是Swift
和Xcode
的新手,所以我的技巧在这里已经用尽了,我不知道这是iOS版本问题,还是一些典型的Xcode
心血来潮.
修改 UITextView
身高的正确方法是什么?
确保在主线程中调用txtresponses.frame=newFrame
。
dispatch_async(dispatch_get_main_queue()) {
txtresponses.frame=newFrame
}
所有 UI 更新必须从主线程完成。
它不起作用,因为我认为您正在使用带有约束的自动布局。请检查下面的 url 这可能对您有帮助 - Change height constraint programmatically
可能是自动布局问题。您只需删除自动布局并检查它是否可以工作。查看下面的代码,希望对您有所帮助。
示例:
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
}
}
屏幕 1:
屏幕 2: