gpt4 book ai didi

ios - 无法获得更改其大小的 View

转载 作者:行者123 更新时间:2023-11-28 05:50:12 25 4
gpt4 key购买 nike

我在 ForecastCell Class 中有一个 UIImageView(对于 UICollectionView),无论如何,我无法获取 UIImageView 在单元格内改变它的大小。这是我尝试过的:

private func setupWeatherIcon(){
self.addSubview(mWeatherIcon)

//mWeatherIcon.frame.size.width = CGFloat(self.frame.width) / 2
//mWeatherIcon.frame.size.height = CGFloat(self.frame.height) / 2
mWeatherIcon.frame.size.width = 20
mWeatherIcon.frame.size.height = 20

mWeatherIcon.centerXAnchor.constraint(equalTo: self.safeAreaLayoutGuide.centerXAnchor).isActive = true
mWeatherIcon.centerYAnchor.constraint(equalTo: self.safeAreaLayoutGuide.centerYAnchor).isActive = true
}

这是 mWeatherIcon:

var mWeatherIcon: UIImageView = {
let image = UIImageView(image: UIImage(named: "partly-cloudy"))

image.translatesAutoresizingMaskIntoConstraints = false
return image
}()

无论我设置什么宽度和高度,它始终保持相同的宽度和高度。

最佳答案

你需要宽度和高度限制

self.contentView.addSubview(mWeatherIcon)

NSLayoutConstraint.activate([
mWeatherIcon.centerXAnchor.constraint(equalTo:contentView.centerXAnchor),
mWeatherIcon.centerYAnchor.constraint(equalTo:contentView.centerYAnchor),
mWeatherIcon.widthAnchor.constraint(equalTo:contentView.widthAnchor,multiplier:0.5),
mWeatherIcon.heightAnchor.constraint(equalTo:contentView.heightAnchor,multiplier:0.5)
])

不要忘记实现这个方法

func collectionView(_ collectionView: UICollectionView, 
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize

关于ios - 无法获得更改其大小的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53240644/

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