具有动态高度的UILabel始终会打印高度0.0

时间:2018-10-13 09:56:05

标签: ios xcode uilabel swift4

我有一个UILabel,可以根据其内容更改其高度。以图形方式,UILabel可以做到这一点,但是当我在代码中打印UILabel高度时,它总是返回0.0。有人可以解释吗?预先感谢。

代码:

private func loadDishDetails() -> () {
        print(HttpRequestUrls.dishes + "/\(selectedDish.getIngredientId())")
        Alamofire.request(HttpRequestUrls.dishes + "/\(selectedDish.getIngredientId())", method: .get, parameters: nil).validate(statusCode: 200..<300).responseString { response in
            response.result.ifSuccess {
                let jsonHttpResponse : JSON = JSON.init(parseJSON: response.value!)
                self.descriptionLabel.text = jsonHttpResponse["description"].stringValue
                for step in jsonHttpResponse["preparationSteps"].arrayValue {
                    self.preparationStepsLabel.text = self.preparationStepsLabel.text! + step.stringValue + "\n"
                }
                self.preparationStepsLabel.layoutIfNeeded()
                self.descriptionLabel.layoutIfNeeded()
                print(self.preparationStepsLabel.frame)
                print(self.descriptionLabel.frame)
                self.viewHeight.constant = self.viewHeight.constant + self.preparationStepsLabel.bounds.size.height
            }
            response.result.ifFailure {
                self.descriptionLabel.text = "Nessuna descrizione"
                self.preparationStepsLabel.text = "Nessuna preparazione"
            }
        }
    }

0 个答案:

没有答案