微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

你如何增加带有 UITextView 的 inputAccessoryView 的高度?

如何解决你如何增加带有 UITextView 的 inputAccessoryView 的高度?

当它内部的 inputAccessoryView 高度增加时,我正在尝试增加我的 UITextView

我偶然发现了这段代码

class InputAccessoryView: UIView,UITextViewDelegate {

    let textView = UITextView()

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

        // This is required to make the view grow vertically
        self.autoresizingMask = UIView.AutoresizingMask.flexibleHeight

        // Setup textView as needed
        self.addSubview(self.textView)
        self.textView.translatesAutoresizingMaskIntoConstraints = false
        self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[textView]|",options: [],metrics: nil,views: ["textView": self.textView]))
        self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[textView]|",views: ["textView": self.textView]))

        self.textView.delegate = self

        // disabling textView scrolling prevents some undesired effects,// like incorrect contentOffset when adding new line,// and makes the textView behave similar to Apple's Messages app
        self.textView.isScrollEnabled = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override var intrinsicContentSize: CGSize {
        // Calculate intrinsicContentSize that will fit all the text
        let textSize = self.textView.sizeThatFits(CGSize(width: self.textView.bounds.width,height: CGFloat.greatestFiniteMagnitude))
        return CGSize(width: self.bounds.width,height: textSize.height)
    }

    // MARK: UITextViewDelegate

    func textViewDidChange(_ textView: UITextView) {
        // Re-calculate intrinsicContentSize when text changes
        self.invalidateIntrinsicContentSize()
    }

}

但我不知道如何添加它。运行它,在第 36 行运行错误。如果它仍然有效,我们如何添加代码(当前使用故事板。UITextViewUIView已连接到不同的文件)。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。