示例项目: http://cl.ly/1l1x1A0J3o2X
我有一个 subview Controller ,里面有一个 UITableView,这个 View Controller 位于另一个 View Controller 之上。我想给它的底部圆角(但只有底部)。
在我的 UITableView 子类中,我有这段代码来圆底角。
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(5.0, 5.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
self.backgroundColor = [UIColor colorWithRed:0.169 green:0.169 blue:0.169 alpha:1];
}
return self;
}
这确实使它们四舍五入,但只要我滚动表格 View ,它就会进一步向上移动并降低高度(至少看起来是这样)。
但是,如果我简单地使用 self.layer.cornerRadius = 5.0
它完全可以正常运行。
有谁知道为什么会出现这种行为?
最佳答案
UIScrollView,以及 UITableView 的子类,实际上是通过更改其呈现内容的边界来呈现其内容。我的猜测是,向 UITableView 的图层添加 mask 实际上是向 View 中的内容添加 mask ,而不是像您想象的那样向容器添加 mask 。
为什么“self.layer.cornerRadius = 5.0”有效,而您的方法无效,我不确定。
对于类似的请求,已经给出了一种更有效的方法,因此我将指导您:
Adding Rounded Corners to only top of UITableView?
基本上,建议是将 UITableView 放入容器中并将 mask 添加到容器中。我相信那会很好。
关于ios - 用 cornerRadius 圆化所有角是可行的,但是如果我只做底部的两个角,它会破坏我的 UITableView 上的滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20587034/