简体   繁体   English

使用自动布局的自定义视图的动态高度

[英]dynamic height of custom view with Autolayout

Actually I am implementing a custom info window in Google Map, I add it with this lines: 实际上,我在Google Map中实现了自定义信息窗口,并使用以下代码添加它:

    //MARK: GMSMapViewDelegate
func mapView(mapView: GMSMapView!, markerInfoWindow marker: GMSMarker!) -> UIView! {
    let infoView : SpMapInfoWindowView = SpMapInfoWindowView.loadFromNibNamed("SpMapInfoWindowView") as! SpMapInfoWindowView;
    infoView.lblLocationName.text = LONG_TEXT;
    return infoView;
}

And my xib layout setting is as following: 我的xib布局设置如下: UI结构 Auto layout part: 自动布局部分: 标签的约束

I want the infowindow's height would increase if the text becomes longer. 我希望如果文本变长,信息窗口的高度将增加。 I've tried a lot with the constraints' priority but the UILabel's height fail to be changed. 我已经对约束的优先级进行了很多尝试,但是UILabel的高度无法更改。

What do I miss? 我想念什么?

Edited 2015-07-02 编辑于2015-07-02

After trying lots of priority and compression resistance, the label would resize but not the superview 在尝试了很多优先级和抗压缩性之后,标签将调整大小,但不会调整超级视图

set numberOfLines of your uilabel to 0. (in IB - 'Lines' property) 将uilabel的numberOfLines设置为0。(在IB-“行”属性中)

Also, try to calculate height of your label programmatically 另外,尝试以编程方式计算标签的高度

label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.ByWordWrapping
label.font = font
label.text = text

label.sizeToFit()

And then set size of your container as label.frame.height + your_margins 然后将容器的大小设置为label.frame.height + your_margins

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM