我是一名Android开发人员,他试图在Xcode上工作,到目前为止它一直很不愉快。我想要做的是拥有一个包含三个子视图的自定义视图:
我希望它能够使内容标签的高度增大和缩小以包装它包含的文本(例如Android' s 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
])
}
据我所知,在我尝试的上述代码中,我将高度设置为常量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开发,因为界面构建器在模拟器中一切正常工作时都没有理由让Xcode崩溃。然后,我做了一些废话编辑,它再次正常工作。
答案 0 :(得分:6)
01)我如何让我的UILabel的高度增长/缩小以匹配其包含的文本?
只需将top,left和right-constraint设置为标签superview。将属性number of lines
设置为0
。然后它将开始包装文本。
02)如何使自定义视图的高度增大/缩小以匹配其包含的子视图?
通过使用界面构建器,这更容易实现。
我的建议是从故事板中的约束开始。您不需要编译代码来查看约束将导致什么。此外,您将直接在界面构建器中收到警告和错误。
如果您想使用编程约束,我的建议是开始使用它的框架。例如:https://github.com/SnapKit/SnapKit
答案 1 :(得分:1)
当代码无缘无故时,通常干净会有很多好处,如果我没记错的话,cmd-shift-k
答案 2 :(得分:1)
您可以使用带有约束的技巧来实现包装内容。例如:
let maximumWidth = frame / 4 //For example
yourView.widthAnchor.constraint(lessThanOrEqualToConstant: maximumWidth).isActive = true
“最大宽度”取决于您的用户界面和设计,可以更改。
此外,您应该在StoryBoard或类似以下代码中设置“ lineBreakMode”:
yourBtn.titleLabel?.lineBreakMode = .byCharWrapping //For UIButton or
yourTxt.textContainer.lineBreakMode = .byCharWrapping //For UITextView