菜鸟教程小白 发表于 2022-12-11 18:40:46

ios - 如何使用自动布局以动态高度居中 View


                                            <p><p>我正在尝试以编程方式创建一个以指定 superView 为中心的 View 。我几乎可以实现这种行为,但是居中 View 不尊重其内容的动态高度。这是我为实现它而创建的函数:</p>

<pre><code>static func overlayWithButton(onView view: UIView, buttonText: String, theme: Theme = .dark, action: Action? = nil) -&gt; UIView {
    // create overlay
    let overlay = UIView()
    overlay.translatesAutoresizingMaskIntoConstraints = false
    let leftInset: CGFloat = 20
    let rightInset: CGFloat = 20

    // Style overlay
    overlay.backgroundColor = theme == .dark ? UIColor.black : UIColor.white
    overlay.alpha = 0.75
    overlay.cornerRadius = 10

    // create button
    let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    button.translatesAutoresizingMaskIntoConstraints = false

    // Style Button
    button.backgroundColor = UIColor.clear
    button.setTitle(buttonText, for: .normal)
    button.setTitleColor(theme == .dark ? lightBlueButtonText : UIColor.black, for: .normal)
    button.titleLabel?.lineBreakMode = .byWordWrapping
    button.titleLabel?.textAlignment = .center
    button.titleLabel?.numberOfLines = 0
    button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
    button.isEnabled = true

    // add button action
    if let action = action {
      button.add(for: .touchUpInside, action)
    }

    // add constraints
    overlay.addSubview(button)
    button.centerXAnchor.constraint(equalTo: overlay.centerXAnchor).isActive = true
    button.leftAnchor.constraint(greaterThanOrEqualTo: overlay.leftAnchor).isActive = true
    button.rightAnchor.constraint(lessThanOrEqualTo: overlay.rightAnchor).isActive = true

    button.topAnchor.constraint(equalTo: overlay.topAnchor).isActive = true
    button.bottomAnchor.constraint(equalTo: overlay.bottomAnchor).isActive = true

    view.addSubview(overlay)
    overlay.leftAnchor.constraint(equalTo: view.leftAnchor, constant: leftInset).isActive = true
    overlay.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -rightInset).isActive = true
    overlay.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

    return overlay
}
</code></pre>

<p>这适用于短按钮标题:
<a href="/image/blyAW.png" rel="noreferrer noopener nofollow"><img src="/image/blyAW.png" alt="it works!"/></a>
但是,对于较长的标题,按钮内容会被剪裁:
<a href="/image/Bwgxv.png" rel="noreferrer noopener nofollow"><img src="/image/Bwgxv.png" alt="enter image description here"/></a>
使用显示我可以看到标题/按钮标签按预期响应。
<a href="/image/BIoGO.png" rel="noreferrer noopener nofollow"><img src="/image/BIoGO.png" alt="it does not work"/></a>
我已经有一段时间了,我无法让居中的叠加层扩展其高度以匹配其内容的内在内容大小。有什么想法吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>经过大量挖掘,发现问题出在 UIButton 与其标签之间的奇怪交互。有关该特定问题的更多详细信息,请参阅 <a href="https://stackoverflow.com/questions/27259333/uibutton-that-resizes-to-fit-its-titlelabel" rel="noreferrer noopener nofollow">this stack overflow post</a>。虽然投票最多的解决方案都没有成功帮助我解决问题,但 <a href="https://stackoverflow.com/a/45618411/3600114" rel="noreferrer noopener nofollow">a solution further down</a> 确实有帮助。解决方案在于创建一个覆盖intrinsicContentSize 和layoutSubviews 的UIButton 子类。此类子类的代码在这里:</p>

<pre><code>class LongTitleButton: UIButton {

    override var intrinsicContentSize: CGSize {
      return self.titleLabel?.intrinsicContentSize ?? super.intrinsicContentSize
    }

    override func layoutSubviews() {
      super.layoutSubviews()
      if let titleLabel = titleLabel {
            titleLabel.preferredMaxLayoutWidth = titleLabel.frame.size.width
      }
    }

}
</code></pre>

<p>另外,由于这个问题似乎特定于 UIButton,因此同样有效的替代解决方案是简单地使用 UILabel 而不是 UIButton,而是在标签上放置一个 UIGestureRecognizer。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何使用自动布局以动态高度居中 View ,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/46206302/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/46206302/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何使用自动布局以动态高度居中 View