gpt4 book ai didi

IOS/objective-C : MakeUILabel Flow to Multiple Lines with Autolayout

转载 作者:行者123 更新时间:2023-11-29 00:01:20 25 4
gpt4 key购买 nike

我试图让 UILabel 流向多行并使用自动布局将其下方的元素向下推。

这是我在 viewwillappear 中调用的代码。

self.myLabel.numberOfLines = 0;
self.myLabel.lineBreakMode = NSLineBreakByWordWrapping;
[_myLabel setContentCompressionResistancePriority:1000
forAxis:UILayoutConstraintAxisVertical];
[self.myLabel setNeedsDisplay];
[self.myLabel layoutIfNeeded];
[self.view setNeedsDisplay];
[self.view layoutIfNeeded];

UILabel 被限制在内容 View 的顶部,它下面的图像被限制在 UILabel 的底部。该标签还具有 >=21 的高度限制。

在初始加载时,标签仅显示一行。

奇怪的是,在启动模态 VC 并关闭它之后,标签确实转到多行但无法下推其下方的元素。

在初始加载时。

enter image description here

启动和取消模态 VC 之后

enter image description here

我想知道问题是否与布局 subview 的时间有关,但我几乎尝试了所有方法。

提前感谢您的任何建议。

最佳答案

简单的例子:

  • 创建一个新的 ViewController。
  • 添加一个按钮,从顶部约束 40 磅并水平居中
  • 添加一个 UIView(绿色 View )。约束水平居中,宽度为 240,按钮的顶部空间为 20。
  • 向绿色 View 添加两个标签。
  • 顶部标签,行数 = 0,前导和尾随约束为 16,顶部约束为 8(到绿色 super View )
  • 底部标签,水平居中,顶部空间到顶部标签 8,底部约束 8(到绿色 super View )。
  • 将顶部标签连接到 View Controller 中的@IBOutlet var multiLineLabel: UILabel!
  • 将按钮 touch-up-inside 连接到 View Controller 中的 @IBAction func didTap(_ sender: Any) 函数。
  • 优先级没有变化。

运行应用程序。每次点击按钮都会向顶部/多行标签添加文本,并且会“下推”底部标签,而底部标签又会“下推”绿色 View 的底部边缘。

enter image description here

class ExpandingLabelViewController: UIViewController {

@IBOutlet var multiLineLabel: UILabel!

@IBAction func didTap(_ sender: Any) {

if let s = multiLineLabel.text {

multiLineLabel.text = s + " Here is some more text."

}

}

}

完成该操作后,您可以将其他元素添加到 View 中。只需确保您有一个垂直间距约束“链”,最顶部的元素被限制在绿色 super View 的顶部,最底部的元素被限制在绿色 super View 的底部。

关于IOS/objective-C : MakeUILabel Flow to Multiple Lines with Autolayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49434441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com