UIView高度变化未在UI中反映

时间:2018-02-24 05:35:53

标签: ios swift uiview autolayout

我有一个包含标签和imageViews的嵌入式堆栈视图的UIView。这个UIView设计用于在其中一个标签的文本达到一定大小时扩展高度。标签设置为lines = 0和自动换行。我已经确认高度会发生变化,但这并未在UI中反映出来。

这是具有标准尺寸名称标签的UIView:

enter image description here

这是带有扩展大小名称标签的UIView。你可以看到"打开"标签被切断:

enter image description here

此函数确定UIView的高度:

    func viewHeight(_ locationName: String) -> CGFloat {

    let locationName = tappedLocation[0].name

    var size = CGSize()

    if let font = UIFont(name: ".SFUIText", size: 17.0) {
        let fontAttributes = [NSAttributedStringKey.font: font]
        size = (locationName as NSString).size(withAttributes: fontAttributes)
    }

    let normalCellHeight = CGFloat(96)
    let extraLargeCellHeight = CGFloat(normalCellHeight + 20.33)

    let textWidth = ceil(size.width)
    let cellWidth = ceil(nameLabel.frame.width)

    if textWidth > cellWidth {
        return extraLargeCellHeight
    } else {
        return normalCellHeight
    }
}

此功能适用于:

    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

    annotation = view.annotation as! MKPointAnnotation

    horizontalStackView.addBackground(color: UIColor.black)

    // Add the tapped location to the tappedLocation array
    for location in locations {
        if location.latitude == annotation.coordinate.latitude && location.longitude == annotation.coordinate.longitude {
            tappedLocation.append(location)
        }
    }

    locationView.frame.size.height = viewHeight(tappedLocation[0].name)
    print("locationView height = \(locationView.frame.height)")
    print("locationView x = \(locationView.frame.origin.x)")
    print("locationView y = \(locationView.frame.origin.y)")

    print("Frame height: \(locationView.frame.size.height)")
    print("Frame widthL \(locationView.frame.size.width)")

    YelpClient.sharedInstance().loadImage(tappedLocation[0].imageUrl, completionHandler: { (image) in

        performUIUpdatesOnMain {

            self.thumbnailImageView.layer.cornerRadius = 10
            self.thumbnailImageView.clipsToBounds = true
            self.thumbnailImageView.layer.borderColor = UIColor.white.cgColor
            self.thumbnailImageView.layer.borderWidth = 1
            self.thumbnailImageView.image = image

            self.nameLabel.text = self.tappedLocation[0].name
            self.nameLabel.textColor = UIColor.white

            self.priceLabel.text = self.tappedLocation[0].price
            self.priceLabel.textColor = UIColor.white

            self.displayRating(location: self.tappedLocation[0])
        }

        YelpClient.sharedInstance().getOpeningHoursFromID(id: self.tappedLocation[0].id, completionHandlerForOpeningHours: { (isOpenNow, error) in

            if let error = error {
                print("There was an error: \(String(describing: error))")
            }

            if let isOpenNow = isOpenNow {

                performUIUpdatesOnMain {

                    if isOpenNow {
                        self.openLabel.text = "Open"
                        self.openLabel.textColor = UIColor.white
                    } else {
                        self.openLabel.text = "Closed"
                        self.openLabel.textColor = UIColor(red: 195/255, green: 89/255, blue: 75/255, alpha: 1.0)
                        self.openLabel.font = UIFont.systemFont(ofSize: 17.0, weight: .semibold)
                    }
                }
            }
        })
    })
    locationView.isHidden = false
}

此print语句指示UIView的高度正在改变高度,但x和y原点不会改变(视图应向上延伸以适应名称标签中的自动换行):

enter image description here

1 个答案:

答案 0 :(得分:1)

手动高度操作在自动布局中不起作用。如果要增加高度,请为高度约束创建IBOutlet并设置其constant值。你甚至可以为它制作动画。