gpt4 book ai didi

ios - 如何为变化的高度添加观察者?

转载 作者:搜寻专家 更新时间:2023-11-01 07:22:32 25 4
gpt4 key购买 nike

我有一个文本框区域,它应该有一个灵活的高度,沿着你写的行数增长。

我想听听它的高度变化,然后当创建新行时,这个听众会说“嗨,高度已经改变了!”。

Initial Height

------------初始高度------------

Height has changed !

------------ 高度变了!!! ----------

我找不到合适的方法来做到这一点,因为 swift 中的 addObserver 需要一个像 keyboardWillShow 这样的 NotificationIdentifier 而这里我没有像 heightWillChange.

当高度改变时如何调用函数?

最佳答案

使用 UITextView 的委托(delegate)方法

var textViewHeight: CGFloat = 0

func textViewDidChange(textView: UITextView)
{
let newTextViewHeight = textView.frame.size.height
if textViewHeight != newTextViewHeight
{
print("---------- Height has changed !!! ----------")
}
textViewHeight = newTextViewHeight
}

关于ios - 如何为变化的高度添加观察者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38057589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com