UITableView - 底部圆角

时间:2016-12-26 06:22:54

标签: ios objective-c uitableview

我正在尝试在UITableView上实现底部圆角。

我使用了下面的代码片段,它存在于MyUtils.m中 -

/*
* To get rounded corners of view.
*/
+ (UIView *)roundCornersOnView:(UIView *)view rectCorner:(UIRectCorner)corner radius:(float)radius
{
   UIView *roundedView = view;
   UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:roundedView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
   CAShapeLayer *maskLayer = [CAShapeLayer layer];
   maskLayer.frame = roundedView.bounds;
   maskLayer.path = maskPath.CGPath;
   roundedView.layer.mask = maskLayer;
   return roundedView;
}

然后我将上面的MyUtils方法从我的swift文件调用到2个视图的圆角。一个是UIView,另一个是UITableView

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
        MyUtils.roundCornersOnView(myUIView, rectCorner: [.TopLeft, .TopRight], radius: 15)
        MyUtils.roundCornersOnView(myTableView, rectCorner: [.BottomLeft, .BottomRight], radius: 15)
} 

UIView没问题,如果是UITableView,我可以看到底部的圆角,我想要实现但是在UITableView开始之后滚动它的内容。

通常UITableView滚动的内容但在应用此UITableView后开始滚动,可能是UITableView滚动的背景。

1 个答案:

答案 0 :(得分:0)

覆盖tableView的掩码层更改其tableViewWrapper属性并引入奇怪的行为,因此我们需要添加容器视图并实现我们的掩码:

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIView *container;
@property (weak, nonatomic) IBOutlet UITableView *testTable;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    CAShapeLayer * maskLayer = [CAShapeLayer layer];
    // manually configuring the mask rect will be the best way to set it rather than setting it in viewDidLayoutSubviews or viewDidAppear that can be called again when view is dismissed or popback.
    CGRect rect = CGRectMake(0, 40, [UIScreen mainScreen].bounds.size.width - 40, [UIScreen mainScreen].bounds.size.height - 204);

    maskLayer.path = [UIBezierPath bezierPathWithRoundedRect: rect byRoundingCorners: UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii: (CGSize){10.0, 10.}].CGPath;

    self.container.layer.mask = maskLayer;
    self.container.clipsToBounds = YES;

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 15;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    cell.textLabel.text = [NSString stringWithFormat:@"%ld", (long)indexPath.row];

    return cell;
}

@end