更改编程创建的自定义InputAccessoryView的高度?Swift 4



所以我正在制作消息传递应用程序,我有4个类属性:

  1. typingView: UIView!
  2. messageTextView: UITextView!
  3. sendButton: UIButton!
  4. typingViewHeight: NSLayoutConstraint?

typingView是主视图的inputAccessoryView,它有效!它包含messageTextViewsendButton。但是我的问题是...当messageTextView中的文本行数增加时,我希望通过动画> 增加高度(我目前忽略了减少(。这样,我决定更改typingView的高度。

检测内容大小的更改功能完美,我添加了一个观察者 添加此行

messageTextView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)

并用这个覆盖

听了
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)

在此功能中,我有(这是一个过度简化和伪代码,只需假设它有效(

UIView.animate(withDuration: keyboardAnimationDuration) {
    self.typingViewHeight?.constaint += (messageTextView.font?.lineHeight)!
}

这是我自定义inputAccessoryView的代码:

lazy var typingViewContainer: UIView = {
    // Set up typing view
    typingView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 55))
    typingView.backgroundColor = UIColor.darkGray
    typingView.translatesAutoresizingMaskIntoConstraints = false
    // Set up animated constraints
    typingViewHeight = typingView.heightAnchor.constraint(equalToConstant: typingView.frame.height)
    typingViewHeight?.isActive = true
    // Set up text view
    messageTextView = UITextView()
    messageTextView.translatesAutoresizingMaskIntoConstraints = false
    messageTextView.font = fakeMessageTextView.font
    messageTextView.keyboardType = fakeMessageTextView.keyboardType
    messageTextView.isScrollEnabled = fakeMessageTextView.isScrollEnabled
    messageTextView.alwaysBounceVertical = fakeMessageTextView.alwaysBounceVertical
    messageTextView.text = fakeMessageTextView.text
    messageTextView.textColor = fakeMessageTextView.textColor
    messageTextView.delegate = self
    typingView.addSubview(messageTextView)
    // Constraints
    messageTextView.bottomAnchor.constraint(equalTo: typingView.bottomAnchor, constant: -fakeMessageTextViewBottom.constant).isActive = true
    messageTextView.topAnchor.constraint(equalTo: typingView.topAnchor, constant: fakeMessageTextViewTop.constant).isActive = true
    messageTextView.leftAnchor.constraint(equalTo: typingView.leftAnchor, constant: fakeMessageTextViewBottom.constant).isActive = true
    messageTextView.widthAnchor.constraint(equalToConstant: fakeMessageTextViewWidth.constant).isActive = true
    // Observers
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)  
    messageTextView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)
    // Set up send button
    sendButton = UIButton(type: fakeSendButton.buttonType)
    sendButton.translatesAutoresizingMaskIntoConstraints = false
    sendButton.setTitle(fakeSendButton.titleLabel?.text!, for: .normal)
    sendButton.titleLabel?.font = fakeSendButton.titleLabel?.font
    sendButton.titleLabel?.shadowColor = fakeSendButton.titleLabel?.shadowColor
    sendButton.addTarget(self, action: #selector(sendPressed(_:)), for: .touchUpInside)
    typingView.addSubview(sendButton)
    // Constraints
    sendButton.heightAnchor.constraint(equalToConstant: fakeSendButtonHeight.constant).isActive = true
    sendButton.widthAnchor.constraint(equalToConstant: fakeSendButtonWidth.constant).isActive = true
    sendButton.bottomAnchor.constraint(equalTo: typingView.bottomAnchor, constant: -fakeSendButtonBottom.constant).isActive = true
    sendButton.rightAnchor.constraint(equalTo: typingView.rightAnchor, constant: -fakeSendButtonRight.constant).isActive = true
    return typingView
}()
override var inputAccessoryView: UIView? {
    get {
        return typingViewContainer
    }
}
override var canBecomeFirstResponder: Bool {
    get {
        return true
    }
}

但是,当我测试并输入多行时,它会在控制台中打印出来:

2018-04-25 08:52:57.614383-0500 ProxiChat[3351:168967] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. 
Try this: 
    (1) look at each constraint and try to figure out which you don't expect; 
    (2) find the code that added the unwanted constraint or constraints and fix it. 
(
"<NSLayoutConstraint:0x608000280a50 UIView:0x7f8528a2cda0.height == 73   (active)>",
"<NSLayoutConstraint:0x6040000993c0 UIView:0x7f8528a2cda0.height == 55   (active)>"
)
Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x608000280a50 UIView:0x7f8528a2cda0.height == 73   
(active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

为什么有两个约束?我只添加了一个。请帮忙,谢谢!

编辑

假设我的更改typingView高度的代码有效,因为它在将视图更改为自定义InputAccessoryView之前起作用。我只想知道为什么它打印出该错误,以及如何从设置两个约束中进行修复,即使我只添加了一个?

创建视图时,您将高度设置为55:

typingView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 55))

然后您设置一个高度锚:

typingViewHeight = typingView.heightAnchor.constraint(equalToConstant: typingView.frame.height)
typingViewHeight?.isActive = true

这就是为什么您看到冲突的原因。另一个观察结果是,当设置高度锚点时,您可以在其创建的同一行中激活它:

typingViewHeight = typingView.heightAnchor.constraint(equalToConstant: typingView.frame.height).isActive = true

这使您无法每次编写额外的代码

第一件事

而不是观察,我建议您使用UITextView的委托方法来调整容器的高度和文本视图。该代码也有相同的技巧,但我认为它更简洁和干净。

func textViewDidChange(_ textView: UITextView) {
        let sizeToFitIn = CGSize(width: textView.bounds.size.width, height: .greatestFiniteMagnitude)
        let newSize = textView.sizeThatFits(sizeToFitIn)
        var newHeight = newSize.height
        ....
        // That part depends on your approach to placing constraints, it's just my example
        textInputHeightConstraint?.constant = newHeight
        inputContainerHeightConstraint?.constant = newHeight + (interfaceFactory.inputContainerHeight - interfaceFactory.textInputMinimumHeight)
    }

尝试这样更新您的代码:

typingViewHeight = typingView.heightAnchor.constraint(equalToConstant: typingView.frame.height)
typingViewHeight?.priority = UILayoutPriority(rawValue: 999)
typingViewHeight?.isActive = true

如果没有帮助,请查看该答案:https://stackoverflow.com/a/27522511/5147552

最新更新