【问题标题】:how can i increase the height of an inputAccessoryView如何增加 inputAccessoryView 的高度
【发布时间】:2015-10-27 15:19:38
【问题描述】:

我花了几天时间解决这个问题。

我有一个inputAccessoryView,它由一个UIView 组成,其中包含一个textView 和两个按钮。 inputAccessoryView 的行为符合预期,并且在除一种情况外的所有情况下都可以正常工作。

当 textView 的高度增加时,我试图将inputAccessoryView 的高度增加相同的量。当我在textViewDidChange 中重新定义inputAccessoryView 的高度时,inputAccessoryView 在键盘上方向下而不是向上增加高度。

我尝试了 SO 的许多不同建议,但没有任何效果。我猜这是inputAccessoryView 中自动添加的NSLayoutConstraint,但我不知道如何在swift 和iOS 8.3 中更改该值。

func textViewDidChange(textView: UITextView) {

    var contentSize = messageTextView.sizeThatFits(CGSizeMake(messageTextView.frame.size.width, CGFloat.max))

    inputAccessoryView.frame.size.height = contentSize.height + 16

}

添加

inputAccessoryView.setTranslatesAutoresizingMaskIntoConstraints(true)

上面的代码有帮助,并且 inputAccessoryView 的高度正确地向上增加,但是我无法同时满足多个约束的约束,并且很难识别违规者。我还得到了一个奇怪的效果,即 textView 在每第二个新行实例的下方创建额外的空间。

谢谢。

【问题讨论】:

  • 我注意到 textViewDidChange 的第一次调用确实在没有 setTranslatesAutoresizingMaskIntoConstraints 行的情况下正确调整了 inputAccessoryView 的大小,但后续调用没有任何效果。我想知道为什么会这样,以及如何每次都将其恢复到初始状态

标签: ios swift nslayoutconstraint autoresizingmask inputaccessoryview


【解决方案1】:

要使输入附件视图垂直增长,您只需设置其autoresizingMask = .flexibleHeight,计算其intrinsicContentSize,然后让框架完成其余工作。

代码:

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]|", options: [], metrics: nil, 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()
    }

}

【讨论】:

  • 感谢您提供非常简洁的回答。
  • 你怎么知道怎么做的?它的工作原理就像魔术一样!我很想知道您学习这项技术所经历的过程。
  • 在 iOS 11 中,此方法仅在输入的文本导致 UITextBox 增长时才有效。收缩时,它保持相同的大小。
  • 以防万一有人是新来的,在当前的 swift 版本 (4.2) 中是 override var intrinsicContentSize: CGSize { ... }autoresizingMask = .flexibleHeight。顺便说一句,答案很好
【解决方案2】:

快进到 2020 年,您只需执行以下操作,其他一切都与 maxkonovalov 的答案相同

override var intrinsicContentSize: CGSize {
    return .zero
}

// MARK: UITextViewDelegate

func textViewDidChange(_ textView: UITextView) {
    sizeToFit()
}

【讨论】:

    猜你喜欢
    • 2018-12-08
    • 2012-01-23
    • 2019-10-24
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 2015-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多