Итак, я создаю приложение для обмена сообщениями, и у меня есть 4 свойства класса:
typingView: UIView!
messageTextView: UITextView!
sendButton: UIButton!
typingViewHeight: NSLayoutConstraint?
typingView
- это inputAccessoryView
основного вида, который работает! Он содержит messageTextView
и sendButton
. Но моя проблема в том, что ... когда количество строк текста в messageTextView
увеличивается, Я хочу, чтобы высота увеличивалась с анимацией (сейчас я игнорирую уменьшение). И, сделав это, я решил изменить высоту typingView
.
Обнаружение изменения в contentSize работает отлично, я добавил наблюдателя
добавив эту строку
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.
Почему существуют два ограничения? Я только добавил один. Пожалуйста, помогите, спасибо!
EDIT
Предположим, что мой код для изменения высоты typingView
работает, поскольку он работал до того, как я изменил свое представление на пользовательский inputAccessoryView. Я просто хочу знать, почему она печатает эту ошибку, и как я могу исправить ее, установив два ограничения, хотя я только добавил одно?