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

ios - 使用 Swift 将 UILabel 动态定位到内容 View 的底部?


                                            <p><p>我试图将我动态创建的 UILabel 定位到我的 contentView 的底部。我正在使用名为 Fuzi 的 HTML Parser 来捕获 HTML 标签并基于它们创建 UILabel;</p>

<pre><code>func stringFromHTML( _ string: String?)
{
    do{
      let doc = try HTMLDocument(string: string!, encoding: String.Encoding.utf8)
      if let root = doc.body {
            for element in root.children {
                if element.tag == &#34;h2&#34; {
                  // Create new label
                  let label = UILabel()
                  label.text = element.stringValue
                  label.numberOfLines = 0
                  label.font = UIFont(name: &#34;Avenir Next&#34;, size: 17)
                  label.textColor = UIColor.black
                  self.contentView.addSubview(label)
                  label.translatesAutoresizingMaskIntoConstraints = false

                  // Label constraints
                  let labelLeading = NSLayoutConstraint(item: label, attribute: .leading, relatedBy: .equal, toItem: contentView, attribute: .leading, multiplier: 1, constant: 20)
                  let labelTrailing = NSLayoutConstraint(item: label, attribute: .trailing, relatedBy: .equal, toItem: contentView, attribute: .trailing, multiplier: 1, constant: -20)
                  let labelTop = NSLayoutConstraint(item: label, attribute: .top, relatedBy: .equal, toItem: contentView, attribute: .top, multiplier: 1, constant: self.contentView.frame.origin.y)

                  self.contentView.addConstraints()

                }
                updateContentViewHeight()
            }
      }

    }
    catch{
      print(&#34;html error\n&#34;,error)
    }

}

func updateContentViewHeight(){
    var totalContentHeight:CGFloat = 0.0
    for i in self.contentView.subviews {
      totalContentHeight += i.frame.height
    }
    let contentViewHeight:NSLayoutConstraint = NSLayoutConstraint(item: self.contentView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: totalContentHeight-29)
    self.contentView.addConstraint(contentViewHeight)

}
</code></pre>

<p>我尝试在 viewDidAppear 中调用我的 stringFromHTML 函数。但它会将我的 UILabel 定位在很远的地方(它们似乎并不存在)。</p>

<p>我想将我的标签放在最后一个标签的底部,见下图;</p>

<p> <a href="/image/CCM53.png" rel="noreferrer noopener nofollow"><img src="/image/CCM53.png" alt="My screen..."/></a> </p>

<p>有人可以帮忙吗?提前致谢!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>改变</p>

<pre><code>let labelTop = NSLayoutConstraint(item: label, attribute: .top, relatedBy: .equal, toItem: contentView, attribute: .top, multiplier: 1, constant: self.contentView.frame.origin.y)
</code></pre>

<p>到</p>

<pre><code>if label != nil {
let labelTop = NSLayoutConstraint(item: label, attribute: .top, relatedBy: .equal, toItem: label, attribute: .top, multiplier: 1, constant: 2)
} else {
let labelTop = NSLayoutConstraint(item: label, attribute: .top, relatedBy: .equal, toItem: contentView, attribute: .top, multiplier: 1, constant: self.contentView.frame.origin.y)
}
</code></pre>

<p>因为您基本上是在强制每个标签位于相同的 y 位置。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 使用 Swift 将 UILabel 动态定位到内容 View 的底部?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40446971/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40446971/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 使用 Swift 将 UILabel 动态定位到内容 View 的底部?