0

我正在尝试在我的应用程序中使用 inputAccessoryViewController,但遇到了更改附件视图高度的问题。我尝试更改视图的框架/边界,还尝试使用约束处理附件视图的高度。但没有什么效果很好。

输入视图控制器代码:

import UIKit
import RxSwift
import RxCocoa


class InputViewController: UIInputViewController {
    private var separatorView: UIView?
    private var answerTextView: ConstrainedTextView?
    private var closeButton: UIButton?
    private var tipLabel: UILabel?

//    private var generalHeightConstraint: NSLayoutConstraint?
    private var separatorHeightConstraint: NSLayoutConstraint?
    private var answerTextViewBottomConstraint: NSLayoutConstraint?

    private let junk = DisposeBag()


    override func viewDidLoad() {
        super.viewDidLoad()

        configureView()
    }

    private func configureView() {

//        view.autoresizingMask = .flexibleHeight

        view.backgroundColor = UIColor.white
        view.frame = CGRect(x: 0, y: 0, width: view.superview?.bounds.width ?? 100, height: 70)
//        view.translatesAutoresizingMaskIntoConstraints = false
//        generalHeightConstraint = AutoLayoutSetAttribute(view, .height, 70)

        // Separator

        separatorView = UIView()
        separatorView?.backgroundColor = UIColor.gray
        separatorView?.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(separatorView!)
        AutoLayoutEqualizeSuper(separatorView, .left, 0)
        AutoLayoutEqualizeSuper(separatorView, .right, 0)
        AutoLayoutEqualizeSuper(separatorView, .top, 0)
        separatorHeightConstraint = AutoLayoutSetAttribute(separatorView, .height, 1)

        // Close Button

        closeButton = UIButton(type: .system)
        closeButton?.setTitle("Hide", for: .normal)
        closeButton?.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        closeButton?.translatesAutoresizingMaskIntoConstraints = false
        closeButton?.addTarget(self, action: #selector(dismissKeyboard), for: .touchUpInside)
        view.addSubview(closeButton!)
        AutoLayoutSetAttribute(closeButton, .width, 70)
        AutoLayoutEqualizeSuper(closeButton, .right, -5)
        view.addConstraint(NSLayoutConstraint(item: closeButton!, attribute: .top, relatedBy: .equal, toItem: separatorView, attribute: .bottom, multiplier: 1, constant: 0))

        // Tip Label

        tipLabel = UILabel()
        tipLabel?.textColor = UIColor.darkGray
        tipLabel?.text = "Input answer:"
        tipLabel?.font = UIFont.systemFont(ofSize: 17)
        tipLabel?.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(tipLabel!)
        AutoLayoutEqualizeSuper(tipLabel, .left, 5)
        AutoLayoutEqualize(tipLabel, separatorView, .top, 0)
        view.addConstraint(NSLayoutConstraint(item: tipLabel!, attribute: .right, relatedBy: .equal, toItem: closeButton, attribute: .left, multiplier: 1, constant: 0))

        // Text View

        answerTextView = ConstrainedTextView()
        answerTextView?.backgroundColor = UIColor.white
        answerTextView?.delegate = self
        answerTextView?.scrollsToTop = false
        answerTextView?.showsVerticalScrollIndicator = false
        answerTextView?.font = REG_FONT(15)
        answerTextView?.maxLines = 5
        answerTextView?.translatesAutoresizingMaskIntoConstraints = false

        answerTextView?.layer.masksToBounds = true
        answerTextView?.layer.cornerRadius = 7

        answerTextView?.layer.borderColor = UIColor.lightGray.withAlphaComponent(0.7).cgColor
        answerTextView?.layer.borderWidth = 1

        view.addSubview(answerTextView!)
        AutoLayoutEqualizeSuper(answerTextView, .left, 5)
        AutoLayoutEqualizeSuper(answerTextView, .right, -5)
        answerTextViewBottomConstraint = AutoLayoutEqualizeSuper(answerTextView, .bottom, -5)
        view.addConstraint(NSLayoutConstraint(item: answerTextView!, attribute: .top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: answerTextView!, attribute: .top, relatedBy: .equal, toItem: closeButton, attribute: .bottom, multiplier: 1, constant: 0))

        answerTextView?
            .rx
            .observe(CGRect.self, "bounds")
            .distinctUntilChanged {
                $0?.size.height == $1?.size.height
            }
            .subscribe { [unowned self] newBounds in
                if var newHeight = newBounds.element??.size.height,
                    let separatorHeight = self.separatorHeightConstraint?.constant,
                    let buttonHeight = self.closeButton?.intrinsicContentSize.height,
                    let bottomSpace = self.answerTextViewBottomConstraint?.constant {

                    newHeight = newHeight == 0 ? 30 : newHeight

                    let generalHeight = newHeight + separatorHeight + buttonHeight + abs(bottomSpace)

                    self.view.frame = CGRect(x: 0, y: 0, width: self.view.superview?.bounds.width ?? 100, height: generalHeight)
//                    self.generalHeightConstraint?.constant = generalHeight

//                    UIView.animate(withDuration: 0.2) {
//                        self.view.setNeedsLayout()
//                        self.view.layoutIfNeeded()
//                    }
                }
            }
            .addDisposableTo(junk)
    }
}


// MARK: - UITextViewDelegate Protocol Conformance

extension InputViewController: UITextViewDelegate {

    func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
        textView.inputAccessoryView = view
        return true
    }

    func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
        textView.inputAccessoryView = nil
        return true
    }

}

使用输入附件 VC 的视图控制器:

import UIKit

class TestViewController: UIViewController {
    override var inputAccessoryViewController: UIInputViewController? {
        return SDAnswerInputViewController()
    }

    override var canBecomeFirstResponder: Bool {
        return true
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

你能解释一下我应该如何正确修改覆盖 inputAccessoryViewController 的输入附件视图的高度吗?

4

1 回答 1

0

问题出在这两行:

view.addConstraint(NSLayoutConstraint(item: answerTextView!, attribute: .top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1, constant: 0))
view.addConstraint(NSLayoutConstraint(item: answerTextView!, attribute: .top, relatedBy: .equal, toItem: closeButton, attribute: .bottom, multiplier: 1, constant: 0))

由于底部和顶部的限制,answerTextView 无法修改其高度。

于 2016-11-03T08:47:52.870 回答