简体   繁体   English

CALayer未在设备/模拟器上显示

[英]CALayer not showing on device / simulator

I've created a custom UITextField which supports partial borders: 我创建了一个自定义UITextField,它支持部分边框:

@import UIKit;

IB_DESIGNABLE
@interface TextField : UITextField

@property (nonatomic, strong) IBInspectable UIColor* borderColor;
@property (nonatomic, assign) IBInspectable CGFloat borderWidth;

@property (nonatomic, assign) IBInspectable BOOL topBorder;
@property (nonatomic, assign) IBInspectable BOOL rightBorder;
@property (nonatomic, assign) IBInspectable BOOL bottomBorder;
@property (nonatomic, assign) IBInspectable BOOL leftBorder;

@end

Each border is implemented as a CALayer: 每个边界都实现为CALayer:

@interface TextField ()

@property (nonatomic, strong) CALayer *topBorderLayer;
@property (nonatomic, strong) CALayer *rightBorderLayer;
@property (nonatomic, strong) CALayer *bottomBorderLayer;
@property (nonatomic, strong) CALayer *leftBorderLayer;

@end

When a BOOL value is set to YES , the relevant layer is created and added as a sublayer to the text field. BOOL值设置为YES ,将创建相关层并将其作为子层添加到文本字段。

Eg if we set topBorder to YES : 例如,如果我们将topBorder设置为YES

- (void)setTopBorder:(BOOL)topBorder
{
    if (topBorder) {
        self.topBorderLayer = [CALayer layer];
        self.topBorderLayer.backgroundColor = [self.borderColor CGColor];
        self.topBorderLayer.frame =
        CGRectMake(0, 0, self.frame.size.width, self.borderWidth);
        [self.layer addSublayer:self.topBorderLayer];
    } else {
        [self.topBorderLayer removeFromSuperlayer];
        self.topBorderLayer = nil;
    }

    _topBorder = topBorder;
}

Now, the question. 现在,这个问题。

This is how IB renders my text field if I set all border properties to YES : 如果我将所有border属性设置为YES这就是IB渲染文本字段的方式:

ibrender

And this is how the device / simulator renders it: 这是设备/模拟器呈现它的方式:

在此处输入图片说明

The right border is not rendering 右边框未渲染

Any idea why? 知道为什么吗? Thanks in advance! 提前致谢!

Source code is available here: Gist 源代码在这里: Gist

Thoughts - I 想法-我

I'm using an empty UIView to create a padding. 我正在使用一个空的UIView创建填充。

 //Padding

 UIView *paddingView = [[UIView alloc] initWithFrame:
                       CGRectMake(0, 0, 15, self.frame.size.height)];
 self.leftView = paddingView;
 self.leftViewMode = UITextFieldViewModeAlways;

Could it be the cause of the issue? 可能是问题的原因吗?

I've switched from CALayer to CoreGraphics . 我已经从CALayer切换到CoreGraphics

It gets the job done. 它完成了工作。

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    if (self.topBorder) {
        CGContextMoveToPoint(context, CGRectGetMinX(rect), CGRectGetMinY(rect));
        CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMinY(rect));
        CGContextSetStrokeColorWithColor(context, [self.borderColor CGColor] );
        CGContextSetLineWidth(context, self.borderWidth);
        CGContextStrokePath(context);
    }

    if (self.rightBorder) {
        CGContextMoveToPoint(context, CGRectGetMaxX(rect), CGRectGetMinY(rect));
        CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMaxY(rect));
        CGContextSetStrokeColorWithColor(context, [self.borderColor CGColor] );
        CGContextSetLineWidth(context, self.borderWidth);
        CGContextStrokePath(context);
    }

    if (self.bottomBorder) {
        CGContextMoveToPoint(context, CGRectGetMaxX(rect), CGRectGetMaxY(rect));
        CGContextAddLineToPoint(context, CGRectGetMinX(rect), CGRectGetMaxY(rect));
        CGContextSetStrokeColorWithColor(context, [self.borderColor CGColor] );
        CGContextSetLineWidth(context, self.borderWidth);
        CGContextStrokePath(context);
    }

    if (self.leftBorder) {
        CGContextMoveToPoint(context, CGRectGetMinX(rect), CGRectGetMaxY(rect));
        CGContextAddLineToPoint(context, CGRectGetMinX(rect), CGRectGetMinY(rect));
        CGContextSetStrokeColorWithColor(context, [self.borderColor CGColor] );
        CGContextSetLineWidth(context, self.borderWidth);
        CGContextStrokePath(context);
    }
}

Now it renders correctly on both IB and device/simulator. 现在,它可以在IB和设备/模拟器上正确呈现。

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

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