gpt4 book ai didi

ios - UIView 高度变化未反射(reflect)在 UI 中

转载 作者:行者123 更新时间:2023-11-28 15:00:43 24 4
gpt4 key购买 nike

我有一个 UIView,带有包含标签和 ImageView 的嵌入式堆栈 View 。此 UIView 旨在当其中一个标签的文本达到特定大小时扩展高度。标签设置为 lines = 0 和自动换行。我已确认高度发生了变化,但这并未反射(reflect)在 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
}

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

enter image description here

最佳答案

手动高度操作在自动布局中不起作用。如果你想增加高度,创建一个 IBOutlet 来限制高度并设置它的 constant 值。您甚至可以为它制作动画。

关于ios - UIView 高度变化未反射(reflect)在 UI 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48959748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com