【问题标题】:UIView Height Change Not Reflected in UIUIView 高度变化未反映在 UI 中
【发布时间】:2018-08-04 05:05:23
【问题描述】:

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

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

这是带有扩展大小名称标签的 UIView。如您所见,“打开”标签被切断:

这个函数决定了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
}

此打印语句表明 UIView 的高度正在改变高度,但 x 和 y 原点没有改变(视图应向上延伸以适应名称标签中的自动换行):

【问题讨论】:

    标签: ios swift uiview autolayout


    【解决方案1】:

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

    【讨论】:

    • 我需要的漂亮而简单的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-02
    相关资源
    最近更新 更多