Xcode swift视图包装内容

Jus*_*tep 12 user-interface ios

我是一名Android开发人员,正在尝试使用Xcode,到目前为止它一直很不愉快.我要做的是拥有一个包含三个子视图的自定义视图:

  • UIImageView(用于图标)
  • UILabel(标题)
  • UILabel(内容)

我想要它,以便内容标签的高度增长和缩小以包装它包含的文本(如Android的wrap_content).然后,我希望自定义视图也可以增长和缩小以包装所有三个子视图.

但是,对于我的生活,我不能弄清楚这些自动布局/约束是如何工作的.

01)我如何使我的UILabel的高度增长/缩小以匹配其包含的文本?

02)如何使我的自定义视图的高度增大/缩小以匹配其包含的子视图?

override func layoutSubviews() {
    super.layoutSubviews()
    img_icon = UIImageView()
    txt_title = UILabel()
    txt_content = UILabel()

    img_icon.backgroundColor = Palette.white
    img_icon.image = icon
    txt_title.text = title
    txt_title.textAlignment = .Center
    txt_title.font = UIFont(name: "Roboto-Bold", size:14)
    txt_title.textColor = Palette.txt_heading1
    txt_content.text = content
    txt_content.textAlignment = .Center
    txt_content.font = UIFont(name: "Roboto-Regular", size:12)
    txt_content.textColor = Palette.txt_dark
    txt_content.numberOfLines = 0
    txt_content.preferredMaxLayoutWidth = self.frame.width
    txt_content.lineBreakMode = NSLineBreakMode.ByWordWrapping
    self.backgroundColor = Palette.white
    addSubview(img_icon)
    addSubview(txt_title)
    addSubview(txt_content)

    /*snip img_icon and txt_title constraints*/

    let txt_content_x = NSLayoutConstraint(item: txt_content, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0)
    let txt_content_y = NSLayoutConstraint(item: txt_content, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 80)
    let txt_content_w = NSLayoutConstraint(item: txt_content, attribute: .Width, relatedBy: .Equal, toItem: self, attribute: .Width, multiplier: 1, constant: 0)
    let txt_content_h = NSLayoutConstraint(item: txt_content, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40)
    txt_content.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activateConstraints([
        txt_content_x,
        txt_content_y,
        txt_content_w,
        txt_content_h
    ])
}
Run Code Online (Sandbox Code Playgroud)

据我所知,在上面的代码中我尝试过,我将高度设置为常量40.这只是因为我不知道如何实现我想要的.

[编辑]

我已经尝试将高度约束设置为大于或等于但它只是崩溃了Xcode.

[编辑]

如果我尝试查看它会崩溃Xcode,但在模拟器中工作得很好.现在的问题是,为什么

我的身高限制现在是:

let txt_content_h = NSLayoutConstraint(item: txt_content, attribute: .Height, relatedBy: .GreaterThanOrEqual, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40)
Run Code Online (Sandbox Code Playgroud)

它在模拟器中工作,具有所需的行为.但是,如果我打开包含视图的故事板,它会崩溃.这绝对是代码行,因为将其更改为.Equal可以解决崩溃问题.

[编辑]

我的临时修复是:

#if TARGET_INTERFACE_BUILDER
    //use .Equal for height constraint
#else
    //use .GreaterThanOrEqual for height constraint
#endif
Run Code Online (Sandbox Code Playgroud)

这样,它不会使Xcode崩溃,仍然在模拟器上呈现我想要的方式.

[编辑]

我删除了预处理器检查,因为我意识到没有像这样定义的实际内容,它现在仍然有用.我发誓我没有改变别的.

这个接近的iOS开发放弃,因为Interface Builder的无保留时,一切都在模拟器中工作的原因崩溃的Xcode.然后,我做了一些废话编辑,它再次正常工作.

ull*_*trm 6

01)我如何使我的UILabel的高度增长/缩小以匹配其包含的文本?

只需将顶部,左侧和右侧约束设置为标签superview.将属性设置number of lines0.然后它将开始包装文本.

02)如何使我的自定义视图的高度增大/缩小以匹配其包含的子视图?

通过使用界面构建器,这更容易实现.

我的建议是从故事板中的约束开始.您不需要编译代码来查看约束将导致什么.此外,您将直接在界面构建器中收到警告和错误.

如果您想使用编程约束,我的建议是开始使用它的框架.例如:https://github.com/SnapKit/SnapKit