Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Xcode swift视图换行内容_Ios_User Interface - Fatal编程技术网

Ios Xcode swift视图换行内容

Ios Xcode swift视图换行内容,ios,user-interface,Ios,User Interface,我是一名Android开发人员,正在尝试Xcode,但到目前为止还不太愉快。我想做的是创建一个自定义视图,它有三个子视图: UIImageView(用于图标) UILabel(用于标题) UILabel(用于内容) 我希望它能够使内容标签的高度增长和收缩,以包装它所包含的文本(就像Android的wrap\u content)。然后,我希望自定义视图也会增长和收缩,以包装所有三个子视图 然而,在我的一生中,我无法理解这些自动布局/约束是如何工作的 01)如何使UILabel的高度增大/缩小以

我是一名Android开发人员,正在尝试Xcode,但到目前为止还不太愉快。我想做的是创建一个自定义视图,它有三个子视图:

  • UIImageView(用于图标)
  • UILabel(用于标题)
  • UILabel(用于内容)
我希望它能够使内容标签的高度增长和收缩,以包装它所包含的文本(就像Android的
wrap\u 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
    ])
}
我知道,在我尝试过的上述代码中,我将高度设置为常量
40
。这只是因为我不知道如何实现我想要的

[编辑]

我尝试将高度约束设置为大于或等于,但它只是破坏了Xcode

[编辑]

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

我的高度限制现在是:

let txt_content_h = NSLayoutConstraint(item: txt_content, attribute: .Height, relatedBy: .GreaterThanOrEqual, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40)
它在模拟器中工作,并具有所需的行为。但是,如果我打开包含该视图的情节提要,它将崩溃。这肯定就是那一行代码,因为将它改回
.Equal
可以解决崩溃问题

[编辑]

我的临时解决办法是:

#if TARGET_INTERFACE_BUILDER
    //use .Equal for height constraint
#else
    //use .GreaterThanOrEqual for height constraint
#endif
这样,它不会使Xcode崩溃,并且仍然以我希望的方式在模拟器上呈现

[编辑]

我删除了预处理器检查,因为我意识到没有定义这样的实际内容,而且它现在仍然有效。我发誓我什么都没改变

我几乎要放弃iOS开发了,因为当一切都在模拟器中运行时,interface builder总是无缘无故地破坏Xcode。然后,我做了一些无意义的编辑,它再次正常工作。

01)如何使UILabel的高度增大/减小以匹配其包含的文本

只需将顶部、左侧和右侧约束设置为标签superview。将属性
行数设置为
0
。然后它将开始包装文本

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
    ])
}

通过使用interface builder,这更容易实现

我的建议是从故事板中的约束开始。您不需要编译代码来查看约束将产生什么结果。此外,您将直接在interface builder中获得警告和错误


如果您想使用编程约束,我的建议是开始使用框架。例如:

当代码无缘无故阻塞时,经常清理会有很多好处。如果我没记错的话,cmd-shift-k

您可以使用带有约束的技巧来实现包装内容。例如:

let maximumWidth = frame / 4  //For example
yourView.widthAnchor.constraint(lessThanOrEqualToConstant: maximumWidth).isActive = true
“最大宽度”取决于您的UI和设计,您可以更改它

此外,您应该在情节提要或类似以下代码中设置“lineBreakMode”:

yourBtn.titleLabel?.lineBreakMode = .byCharWrapping //For UIButton or
yourTxt.textContainer.lineBreakMode = .byCharWrapping //For UITextView

谢谢你的建议,伙计。我认为唯一的方法是通过编程将首选宽度设置为superview的宽度。我没想到。我现在唯一的问题是,接口生成器和Xcode将反复、任意地决定停止工作,直到我转换一些不影响任何内容的代码。它每隔几分钟就会发生一次,我可以继续重新启动Xcode,直到我决定改变一些代码,比如将高度从“45”改为“125”或类似的随机值,它才起作用。或者,正如在我的编辑中所看到的,在一个不存在的值上添加一个检查,然后删除。“通过使用界面生成器,这更容易实现。”但我并不清楚如何做到这一点,而且似乎没有一种方法可以实现Android对文本视图的“包装内容”。“约束”系统只能指定父窗口的百分比。这更像是注释而不是答案。