iOS Swift 单元格内的圆形视图显示不正确
iOS Swift round views inside cell show incorrectly
晚上,我有日历collection。
这些单元格有一些圆形视图在第一次显示时不正确,但是当它们重新加载时它们显示正确。
我知道问题是细胞第一次不知道框架的正确大小。
我尝试过的:
1-调用里面的round函数layoutSubviews()
:只有右侧正确四舍五入
2 - 在 cellWillLayout
中调用 round 函数:没有变化
这是舍入函数:
func makeRound() {
print("rounding")
currentDayView.layer.cornerRadius = currentDayView.frame.height/2
currentDayView.layer.masksToBounds = true
currentDayView.clipsToBounds = true
selectedDayView.layer.cornerRadius = selectedDayView.frame.height/2
selectedDayView.layer.masksToBounds = true
selectedDayView.clipsToBounds = true
}
有什么建议吗?
圆角的最佳位置是在每个视图的 layoutSubviews
中,或者(例如,如果您没有 sub-classed 它们)将其放在视图控制器中 viewDidLayoutSubviews
.
您案例中的每个视图都是 currentDayView
和 selectedDayView
的 layoutSubviews
。
您需要覆盖 layoutSubviews
方法,然后在其中调用您的方法:
override func layoutSubviews() {
super.layoutSubviews()
self.makeRound()
}
晚上,我有日历collection。 这些单元格有一些圆形视图在第一次显示时不正确,但是当它们重新加载时它们显示正确。
我知道问题是细胞第一次不知道框架的正确大小。
我尝试过的:
1-调用里面的round函数layoutSubviews()
:只有右侧正确四舍五入
2 - 在 cellWillLayout
中调用 round 函数:没有变化
这是舍入函数:
func makeRound() {
print("rounding")
currentDayView.layer.cornerRadius = currentDayView.frame.height/2
currentDayView.layer.masksToBounds = true
currentDayView.clipsToBounds = true
selectedDayView.layer.cornerRadius = selectedDayView.frame.height/2
selectedDayView.layer.masksToBounds = true
selectedDayView.clipsToBounds = true
}
有什么建议吗?
圆角的最佳位置是在每个视图的 layoutSubviews
中,或者(例如,如果您没有 sub-classed 它们)将其放在视图控制器中 viewDidLayoutSubviews
.
您案例中的每个视图都是 currentDayView
和 selectedDayView
的 layoutSubviews
。
您需要覆盖 layoutSubviews
方法,然后在其中调用您的方法:
override func layoutSubviews() {
super.layoutSubviews()
self.makeRound()
}