我有一个自定义的 UITableViewCell
,其背景为 UIView
,顶部为 UITextView
。
UIView
用于将背景颜色设置为白色,并使用圆角边缘来显示单元格的 contentView.backgroundColor
(红色)在角落。
当用户输入文本时,UITextView
高度会正确扩展。
UIView
(在屏幕截图中突出显示)在与 UITableViewCell 一起增长的所有 4 条边上都有限制。这可以正常工作(参见屏幕截图,我还检查了框架高度)。
问题是白色的UIView.backgroundColor
没有展开,所以“展开”的部分是透明的,让红色的contentView.backgroundColor
显示出来。
我尝试在 UITextViewDelegate textViewDidChange
中的 UIView
和/或 UITableViewCell
上调用 setNeedsLayout
,但是这并没有解决问题。
此外,在 tableView:cellForRowAtIndexPath:
或 tableView:willDisplayCell:forRowAtIndexPath
中设置背景颜色不会产生任何影响。
这似乎与此类似SO post .
如何让 UIView backgroundColor
在整个更新、扩展的框架上绘制,而不仅仅是原始框架?
2018-06-20 更新:
这是我正在做的以获得圆角的方法(来自this SO post)
// from http://stackoverflow.com/questions/4847163/round-two-corners-in-uiview/14485362
- (void)setMaskTo:(id)sender byRoundingCorners:(UIRectCorner)corners withCornerRadii:(CGSize)radii {
// UIButton requires this
[sender layer].cornerRadius = 0.0;
UIBezierPath *shapePath = [UIBezierPath bezierPathWithRoundedRect:[sender bounds]
byRoundingCorners:corners
cornerRadii:radii];
CAShapeLayer *newCornerLayer = [CAShapeLayer layer];
newCornerLayer.frame = [sender bounds];
newCornerLayer.path = shapePath.CGPath;
[sender layer].mask = newCornerLayer;
}
然后,在 tableView:cellForRowAtIndexPath:
中,我用 来调用它
[self setMaskTo:textInputCell.bgColorView byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) withCornerRadii:CORNER_RADII];
最佳答案
如评论中所述,如果您的目标是 iOS 11+,则可以使用 CACornerMask
(layer.maskedCorners
),它允许您指定要圆角的特定角。
如果您需要支持 iOS 10 或更早版本,您可以将您的 UIBezierPath
代码放入 layoutSubviews()
- 这样,您可以随时更新其框架 View 的框架变化。
关于ios - 当框架更改时,UIView 背景颜色不会重绘(在动态扩展的 TableViewCell 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50926215/