gpt4 book ai didi

ios - Swift - UICollectionReusableView 的 subview 不适合宽度

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

我有一个带有 UICollectionReusableView( header )的 UICollectionViewController。在这个 UICollectionReusableView 中,我有一个 subview (粉红色)。

enter image description here

我已经为 subview 设置了一些约束,一个全屏宽度。

它非常适合背景 img(黑色)。但是,关于 subview ,它仅适用于 iPhone 6 尺寸(例如,对于 iPhone 5s 来说太大,对于 iPhone 6 plus 来说不够)。我真的不知道为什么它适用于背景图像而不适用于此 subview 。粉色 subview 是“Radius View on Top”,背景图片是“Ban img”。

我尝试在 UICollectionReusableView 的 awakeFromNib() 中对此进行编码

let width = UIScreen.main.bounds.size.width
self.widthConstraintRadiusView.constant = width

编辑

我发现了一些东西:

enter image description here

我可以看到 subview 具有正确的宽度,但没有拉伸(stretch)?这怎么可能?我必须了解什么?

enter image description here

编辑2

如果我在 subview 上使用函数 roundCorners,它不起作用:

func roundCorners(_ corners:UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}

radiusViewOnTop.roundCorners([.topLeft,.topRight], radius: 7)

我怎样才能保持功能并具有正确的宽度?

最佳答案

根据您的最新编辑,我知道为什么它不起作用,这是一个简单的修复。

您的 View 大小正确,但您添加的 mask 大小错误。

您使用 iPhone 6 尺寸设计界面,这些尺寸是加载 View 的尺寸。之后,它会对您的 View 执行布局传递,以将大小更新为实际大小。

但是,您可能在 awakeFromNib 或类似的东西上调用圆角方法,这发生在 View 加载之后但大小更新之前。

修复很简单,重写layoutSubviews,先调用super,再调用roundCorners:方法。

关于ios - Swift - UICollectionReusableView 的 subview 不适合宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47576272/

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