ios - 在 UITableview 中绘制带外部阴影的边框

标签 ios objective-c

这是我设计的列表项。列表中有多个项目,如 tableview enter image description here只是有一个矩形项目,它具有非常细的边框和外部阴影。

我试过用这种方式把这个设计转化成代码。 enter image description here

节标题、描述和主管是该节中的单元格数。

我试过这段代码。

-(void)drawLeftBorder : (UIView *)view{

CALayer *leftBorder = [CALayer layer];
leftBorder.backgroundColor = [UIColor colorWithRed:221/255.0f green:221/255.0f blue:221/255.0f alpha:1].CGColor;
leftBorder.frame = CGRectMake(0,0,1,view.frame.size.height+1);

leftBorder.shadowColor =   [UIColor grayColor].CGColor;
leftBorder.shadowOffset = CGSizeMake(0, 0);
leftBorder.shadowOpacity = 1;
leftBorder.shadowRadius = 5;

[view.layer addSublayer:leftBorder];
}

-(void)drawRightBorder : (UIView *)view{

CALayer *rightBorder = [CALayer layer];
rightBorder.backgroundColor = [UIColor colorWithRed:221/255.0f green:221/255.0f blue:221/255.0f alpha:1].CGColor;
rightBorder.frame = CGRectMake(view.frame.size.width-1,0,1,view.frame.size.height+1);

rightBorder.shadowColor =   [UIColor grayColor].CGColor;
rightBorder.shadowOffset = CGSizeMake(0, 3);
 rightBorder.shadowOpacity = 1;
rightBorder.shadowRadius = 5;


[view.layer addSublayer:rightBorder];
}



-(void)drawTopBorder : (UIView *)view{

CALayer *topBorder = [CALayer layer];
topBorder.backgroundColor = [UIColor colorWithRed:221/255.0f green:221/255.0f blue:221/255.0f alpha:1.0f].CGColor;
topBorder.frame = CGRectMake(0,0,view.frame.size.width,1.0);


topBorder.shadowColor =   [UIColor grayColor].CGColor;
//topBorder.shadowColor = [UIColor grayColor].CGColor;
topBorder.shadowOffset = CGSizeMake(0, 0);
topBorder.shadowOpacity = 1;
topBorder.shadowRadius = 5;


[view.layer addSublayer:topBorder];
}

enter image description here

  • 首先,画线似乎没问题。
  • 阴影应该在截面的外部。
  • 节后和单元格之间有阴影中断 还有。

最佳答案

无需使用 CALayer 或 CAShape Layer,我们可以直接操作该部分的图层来显示边框和阴影。

UITableViewHeaderFooterView *section = [tblView 
   dequeueReusableHeaderFooterViewWithIdentifier: @"identifier"];
    [section.layer setMasksToBounds: false];

    [[UIColor grayColor] CGColor];

    //Adding border to your section
    [section.layer setBorderWidth: 1.0];  //Change Border Width here
    [section.layer setBorderColor: [[UIColor grayColor] CGColor]];  //Change Border Color here

    //Adding shadow to the section
    [section.layer setShadowColor: [[UIColor darkGrayColor] CGColor]];  //Change Shadow Color here
    [section.layer setShadowOffset: CGSizeZero];   //Change Shadow Offset i.e shadow's displacement from section
    [section.layer setShadowRadius: 3.5];    //Change Shadow Radius i.e how far the shadow will spread
    [section.layer setShadowOpacity: 0.50];   //Change visibility of shadow here

关于ios - 在 UITableview 中绘制带外部阴影的边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51888899/

相关文章:

ios - RestKit: bool 值

ios - UIWindow 无法填满新设备的屏幕?

ios - 处理 NSURLSessionDownloadTask 失败

objective-c - objective-c 中的所有属性的 setter/getter 都一样吗?

ios - 快速询问初始 View Controller 的音乐权限

ios - 地标 kml 信息未显示在 iOS 版 Google 地球中

ios - 如何声明符合协议(protocol)的显式类型?

ios - OpenWebRTC iOS 随机崩溃 gstglcontext

objective-c - 'convertPoint' 的使用不明确

objective-c - 如何在 SpriteKit 游戏中获取键盘输入?