[英]How to convert tableview cell to rounded corner with shadow in iOS 9.0 or later
要显示圆角单元,可以设置角半径。
对于当前设计,您可以创建一个自定义UItableViewCell
并UItableViewCell
设置拐角半径。
yourCustomCell.layer.cornerradius = 6.0f
在该UItableViewCell
您需要根据需要添加其他标签。
如果您使用普通表格视图单元格
cell.contentView.layer.cornerRadius = 7.0f;
cell.contentView.layer.masksToBounds = YES;
如果使用自定义表格视图单元格
cell.layer.cornerRadius = 7.0f;
cell.layer.masksToBounds = YES;
在故事板上清除UITableView的背景颜色。 然后,您将能够看到更改。
对于细胞行高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44; //It default size.If you want to change to other size you can change.
}
尝试此步骤,您将显示图像,例如:
您将添加一个视图,顶部= 8,底部= 8,左侧= 16,右侧= 16在单元格内容视图中设置约束。
view.layer.cornerradius = 5.0f;
表背景颜色设置清晰。
如果要在UITableviewCell中实现圆角和阴影,则需要在单元中实现一个(自定义)UIView。
一种方法是:
@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;
}
将自定义视图添加到表视图单元格并设置以下类:
CustomView.h
#import <UIKit/UIKit.h>
#import "Common.h"
@interface CustomView : UIView
{
IBInspectable UIColor *startColor;
IBInspectable UIColor *endColor;
}
@end
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
#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
#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.