分割线 - layer

对于一些没有事件响应的视图,比如分割线,我们可以直接添加layer来达到显示效果:

#import 

@interface UIView (HTExtension)


- (void)addLineLayerWithX:(CGFloat)x Y:(CGFloat)y Width:(CGFloat)width Height:(CGFloat)height Color:(UIColor *)color;


@end
#import "UIView+HTExtension.h"

@implementation UIView (HTExtension)

- (void)addLineLayerWithX:(CGFloat)x Y:(CGFloat)y Width:(CGFloat)width Height:(CGFloat)height Color:(UIColor *)color{

    CALayer * lineLayer = [CALayer layer];
    lineLayer.position = CGPointMake(x, y);
    lineLayer.anchorPoint = CGPointZero;
    lineLayer.bounds = CGRectMake(0, 0, width, height);
    lineLayer.backgroundColor = color.CGColor;
    [self.layer addSublayer:lineLayer];
}

@end

你可能感兴趣的:(iOS-OC)