【问题标题】:UITableView - Bottom Rounded CornersUITableView - 底部圆角
【发布时间】:2016-12-26 06:22:54
【问题描述】:

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

我使用了下面的代码 sn-p,它存在于我的 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;
}

然后我从我的 swift 文件中调用上面的 MyUtils 方法到 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 的背景正在滚动。

【问题讨论】:

  • 将 clipToBounds 应用到您的 myTableView,这样超出 myTableView 范围的内容将不可见。
  • @nferocious76 感谢您对此进行调查,但不确定这没有用
  • 可以给我看那部分的截图吗?
  • 请加入这个房间:join.skype.com/OnghTaGAf3VB
  • 请检查文件:)

标签: ios objective-c uitableview


【解决方案1】:

覆盖 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

【讨论】:

  • 我们可以将UIView设置为Table的backgroundView,不引入新的父view就不能更新那个backgroundView吗?
  • 没有。每当你滚动它时,backgroundVew 都会改变它的偏移量,虽然你也可以手动调整它的大小,但它位于 tableView 的内容后面,所以你的角会在下面并且没用。
猜你喜欢
  • 2015-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多