Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift 3:UITextView-动态高度-编程方式_Ios_Swift_Keyboard_Uitextview - Fatal编程技术网

Ios Swift 3:UITextView-动态高度-编程方式

Ios Swift 3:UITextView-动态高度-编程方式,ios,swift,keyboard,uitextview,Ios,Swift,Keyboard,Uitextview,我有一个keyboardContainer类(UIView/的子类,通过编程创建,因此没有故事板),其中包括一个UITextView,供用户键入消息。它在聊天日志类中使用,并设置为inputAccessoryView。我想在用户键入时动态更改它的高度 我寻找答案,找到了一些。然而,我没有得到大多数,因为它们不为我工作 我必须实施什么才能达到我想要的效果 谢谢你的帮助 编辑: 首先感谢你的帮助 然而,我对编码相当陌生,所以我无法解决这个问题。我想这与我创建keyboardContainer类的方式

我有一个keyboardContainer类(UIView/的子类,通过编程创建,因此没有故事板),其中包括一个UITextView,供用户键入消息。它在聊天日志类中使用,并设置为inputAccessoryView。我想在用户键入时动态更改它的高度

我寻找答案,找到了一些。然而,我没有得到大多数,因为它们不为我工作

我必须实施什么才能达到我想要的效果

谢谢你的帮助

编辑:

首先感谢你的帮助

然而,我对编码相当陌生,所以我无法解决这个问题。我想这与我创建keyboardContainer类的方式及其约束有关

以下是我的键盘容器类中的相关代码:

 let textField:UITextView = {
    let view = UITextView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.layer.cornerRadius = 15
    view.layer.masksToBounds = true
    view.font = UIFont.systemFont(ofSize: 15)
    view.backgroundColor = .white
    return view
}()

overried init(frame: CGRect){
super.init(frame: frame)

addSubview(textField)
    textField.leftAnchor.constraint(equalTo: leftButton, constant: 5).isActive = true
    textField.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true

    textFieldHeightAnchor = textField.heightAnchor.constraint(equalTo: heightAnchor, constant: -10)
    textFieldHeightAnchor.isActive = true

    textFieldRightAnchor = textField.rightAnchor.constraint(equalTo: rightAnchor, constant: -85)
    textFieldRightAnchor.isActive = true
}
在我的聊天日志中,我使用以下内容:

 lazy var keyboard: KeyboardContainer = {
    let key = KeyboardContainer()
    key.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 45)
    key.sendButton.addTarget(self, action: #selector(handleSend), for: .touchUpInside)
    return key
}()

override var inputAccessoryView: UIView?{
    get{
        return keyboard
    }
}

我需要改变什么?我猜限制条件是什么?

只需禁用滚动即可。不要设置任何其他约束。这可能对你有所帮助

在viewDidLoad方法中

    YourTextView.translatesAutoresizingMaskIntoConstraints = true
    YourTextView.sizeToFit()
    YourTextView.isScrollEnabled = false
    YourTextView.delegate = self
    YourTextView.isEditable = true
然后使用UITextViewDelegate和

func textViewDidChange(_ textView: UITextView) {

    MyTextView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 100)
    MyTextView.translatesAutoresizingMaskIntoConstraints = true
    MyTextView.sizeToFit()
    MyTextView.isScrollEnabled = false

}
这是变化之前,,

这是在,
只需禁用滚动即可。不要设置任何其他约束。这可能对你有所帮助

在viewDidLoad方法中

    YourTextView.translatesAutoresizingMaskIntoConstraints = true
    YourTextView.sizeToFit()
    YourTextView.isScrollEnabled = false
    YourTextView.delegate = self
    YourTextView.isEditable = true
然后使用UITextViewDelegate和

func textViewDidChange(_ textView: UITextView) {

    MyTextView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 100)
    MyTextView.translatesAutoresizingMaskIntoConstraints = true
    MyTextView.sizeToFit()
    MyTextView.isScrollEnabled = false

}
这是变化之前,,

这是在,

您可以使textView符合UITextViewDelegate,然后将其调整为contentSize

使视图控制器符合委托

class ViewController: UIViewController, **UITextViewDelegate** { ... 
之后

yourTextView.delegate = self // put that in viewDidLoad()
然后可以实现textViewDidChange方法。也就是说,每次你在键盘上输入东西时,这个函数就被调用

func textViewDidChange(_ textView: UITextView) {


if textView == youTextView {

    let currentHeight = textView.frame.size.height

    textView.frame.size.height = 0 // you have to do that because if not it's not working with the proper content size

    textView.frame.size = textView.contentSize // here you detext your textView's content size and make it resize.

    let newHeight = textView.frame.size.height

    let heightDifference = newHeight - currentHeight // get the height difference from before and after editing

    yourContainerView.frame.size.height += heightDifference

}

}

您可以使textView符合UITextViewDelegate,然后将其调整为contentSize

使视图控制器符合委托

class ViewController: UIViewController, **UITextViewDelegate** { ... 
之后

yourTextView.delegate = self // put that in viewDidLoad()
然后可以实现textViewDidChange方法。也就是说,每次你在键盘上输入东西时,这个函数就被调用

func textViewDidChange(_ textView: UITextView) {


if textView == youTextView {

    let currentHeight = textView.frame.size.height

    textView.frame.size.height = 0 // you have to do that because if not it's not working with the proper content size

    textView.frame.size = textView.contentSize // here you detext your textView's content size and make it resize.

    let newHeight = textView.frame.size.height

    let heightDifference = newHeight - currentHeight // get the height difference from before and after editing

    yourContainerView.frame.size.height += heightDifference

}

}

如果您正在寻找类似消息应用程序的
UITextView
。 不再需要处理代码,您可以使用约束来管理它。 使用约束比处理更多的编码更好

下面是对此的逐步解释

步骤:1

  • UItextView
    ui按钮
    安排在一个
    UIView
    内,并以低优先级为
    UIView
    提供高度约束

步骤:2

  • 还为
    UITextView
    提供大于或等于的高度约束。正如图像所代表的那样
步骤:3

现在您只需处理
contentSize
iscrowlenabled
,如下代码片段所示

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool{
if (textView.contentSize.height < 150){
    textView.isScrollEnabled = false
   }
   else{
     textView.isScrollEnabled = true
   }
  return true
}
func textView(textView:UITextView,shouldChangeTextIn范围:NSRange,replacementText:String)->Bool{
如果(textView.contentSize.height<150){
textView.IsCrollenabled=false
}
否则{
textView.IsCrollenabled=true
}
返回真值
}

希望这对你有帮助

如果您正在寻找类似消息应用的
UITextView
。 不再需要处理代码,您可以使用约束来管理它。 使用约束比处理更多的编码更好

下面是对此的逐步解释

步骤:1

  • UItextView
    ui按钮
    安排在一个
    UIView
    内,并以低优先级为
    UIView
    提供高度约束

步骤:2

  • 还为
    UITextView
    提供大于或等于的高度约束。正如图像所代表的那样
步骤:3

现在您只需处理
contentSize
iscrowlenabled
,如下代码片段所示

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool{
if (textView.contentSize.height < 150){
    textView.isScrollEnabled = false
   }
   else{
     textView.isScrollEnabled = true
   }
  return true
}
func textView(textView:UITextView,shouldChangeTextIn范围:NSRange,replacementText:String)->Bool{
如果(textView.contentSize.height<150){
textView.IsCrollenabled=false
}
否则{
textView.IsCrollenabled=true
}
返回真值
}

希望这对你有帮助

@khtm125不需要在键入UITextView时更改高度。“文本视图”是“滚动视图”的子类。如果用户写入的信息超过一行,我希望“文本视图”显示的信息不止一行。@khtm125在键入UITextView时更改高度不是必需的。“文本视图”是“滚动视图”的子类。如果用户写入的内容超过一行,我希望“文本视图”显示的信息不止一行。这实际上不会改变“文本视图”的大小。@matthris它实际上可以工作。检查所附的屏幕截图。这实际上不会改变文本视图的大小。@它实际上可以工作。查看附件中的截图。谢谢您的回答!这对我不起作用,所以我编辑了我的问题。。。你知道如何解决这个问题吗?谢谢你的回答!这对我不起作用,所以我编辑了我的问题。。。你知道如何解决这个问题吗?