简体   繁体   English

如何在iOS 9.0或更高版本中将tableview单元格转换为带有阴影的圆角

[英]How to convert tableview cell to rounded corner with shadow in iOS 9.0 or later

在此处输入图片说明

I want to show tableview cell like above image. 我想显示上图的表格单元格。 Please help me out. 请帮帮我。

To show rounded corner cell you can set corner radius. 要显示圆角单元,可以设置角半径。

For the current design you can create a custom UItableViewCell and set corner radius to it. 对于当前设计,您可以创建一个自定义UItableViewCellUItableViewCell设置拐角半径。

yourCustomCell.layer.cornerradius = 6.0f

In that UItableViewCell you need to add other labels as per your requirement. 在该UItableViewCell您需要根据需要添加其他标签。

If you use normal table view cell 如果您使用普通表格视图单元格

cell.contentView.layer.cornerRadius = 7.0f;
cell.contentView.layer.masksToBounds = YES;

If you use custom table view cell 如果使用自定义表格视图单元格

cell.layer.cornerRadius = 7.0f;
cell.layer.masksToBounds = YES;

clear background color of UITableView in storyboard. 在故事板上清除UITableView的背景颜色。 Then you will be able to see the change. 然后,您将能够看到更改。

For Cell Row Height 对于细胞行高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  return 44;  //It default size.If you want to change to other size you can change.
}

Try this step you display image like: 尝试此步骤,您将显示图像,例如:

you are add one view and top = 8, bottom = 8, left = 16, right = 16 set constraint in cell content view. 您将添加一个视图,顶部= 8,底部= 8,左侧= 16,右侧= 16在单元格内容视图中设置约束。

view.layer.cornerradius = 5.0f;

table background color set clear. 表背景颜色设置清晰。

If you want rounded corners and shadow in an UITableviewCell you need to implement a (custom) UIView in the cell. 如果要在UITableviewCell中实现圆角和阴影,则需要在单元中实现一个(自定义)UIView。

One way doing that is: 一种方法是:

@interface customCell ()

@property (strong, nonatomic) IBOutlet UIView *someView;

@end

@implementation customCell

- (void)awakeFromNib {
    [super awakeFromNib];

    //Set the corner radius
    self.someView.layer.cornerRadius = 5.0f;

    // Add shadow
    [self applyShadow];
}
- (void)applyShadow {
    // Add shadow
    [self.layer setShadowColor:[[UIColor blackColor] CGColor]];
    [self.layer setShadowOffset:CGSizeMake(0, 2)];
    [self.layer setShadowRadius:2.0];
    [self.layer setShadowOpacity:0.5];
    self.clipsToBounds = false;
    self.layer.masksToBounds = false;
}

Add the custom View to the table View cell and set the below class: 将自定义视图添加到表视图单元格并设置以下类:

CustomView.h CustomView.h

#import <UIKit/UIKit.h>
#import "Common.h"
@interface CustomView : UIView
{
    IBInspectable UIColor *startColor;
    IBInspectable UIColor *endColor;
}
@end

CustomView.m CustomView.m

#import "CustomView.h"
IB_DESIGNABLE
@implementation CustomView
- (void)drawRect:(CGRect)rect
{
    CGContextRef context=UIGraphicsGetCurrentContext();
    UIColor *ShadowColor=[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1.0];
    UIColor *backGround=[UIColor whiteColor];
    CGFloat outerMagian=2.0;
    CGRect outerRect=CGRectInset(self.bounds, outerMagian, outerMagian);
    CGPathRef outerPath=createRoundedRectForRect(outerRect, 0.0);
    CGContextSaveGState(context);
        CGContextSetShadowWithColor(context, CGSizeMake(2, 2), 3.0, ShadowColor.CGColor);
        CGContextSetFillColorWithColor(context, backGround.CGColor);
       CGContextAddPath(context, outerPath);
    CGContextFillPath(context);
    CGContextRestoreGState(context);
    CGFloat borderMarian=1.0;
    CGRect borderRect=CGRectInset(self.bounds, borderMarian, borderMarian);
    CGPathRef borderPath=createRoundedRectForRect(borderRect, 0.0);
    CGContextSaveGState(context);
    CGContextSetLineWidth(context, 2.0);
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:21.0/255.0 green:92.0/255.0 blue:136.0/255.0 alpha:1.0].CGColor);
    CGContextAddPath(context, borderPath);
    CGContextStrokePath(context);
    CGContextRestoreGState(context);
    CGPathRelease(outerPath);
    CGPathRelease(borderPath);
    UIBezierPath *path=[UIBezierPath bezierPathWithRoundedRect:outerRect cornerRadius:0.0];
    [path addClip];
    drawLinearGradient(context,outerRect, startColor.CGColor, endColor.CGColor);
}

@end

Common.h COMMON.H

#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
@interface Common : NSObject
CGMutablePathRef createRoundedRectForRect(CGRect rect, CGFloat radius);
void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor);
@end

Common.m Common.m

#import "Common.h"

@implementation Common
CGMutablePathRef createRoundedRectForRect(CGRect rect, CGFloat radius)
{

    CGMutablePathRef path=CGPathCreateMutable();

    CGPathMoveToPoint(path, NULL, CGRectGetMidX(rect), CGRectGetMinY(rect));
    CGPathAddArcToPoint(path, NULL, CGRectGetMaxX(rect), CGRectGetMinY(rect), CGRectGetMaxX(rect), CGRectGetMaxY(rect), radius);
    CGPathAddArcToPoint(path, NULL, CGRectGetMaxX(rect), CGRectGetMaxY(rect), CGRectGetMinX(rect), CGRectGetMaxY(rect), radius);
    CGPathAddArcToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMaxY(rect), CGRectGetMinX(rect), CGRectGetMinY(rect), radius);
    CGPathAddArcToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMinY(rect), CGRectGetMaxX(rect), CGRectGetMinY(rect), radius);
    CGPathCloseSubpath(path);
    return path;
}
void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor)
{
    CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
    CGFloat locations[]={0.0,1.0};
    NSArray *color=@[(__bridge id)startColor,(__bridge id)endColor];
    CGGradientRef gradient=CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)color, locations);

    CGPoint startPoint=CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
    CGPoint endPoint=CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));

    CGContextSaveGState(context);
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);

    CGContextRestoreGState(context);
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorSpace);
}

@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM