gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:55:43 26 4
gpt4 key购买 nike

我创建了一个支持部分边框的自定义 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

每个边框都作为 CALayer 实现:

@interface TextField ()

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

@end

BOOL 值设置为 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;
}

现在,问题。

如果我将所有边框属性设置为 YES,这就是 IB 呈现我的文本字段的方式:

ibrender

这就是设备/模拟器呈现它的方式:

enter image description here

右边框未渲染

知道为什么吗?提前致谢!

此处提供源代码:Gist

想法 - 我

我正在使用一个空的 UIView 来创建一个填充。

 //Padding

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

这可能是问题的原因吗?

最佳答案

我已从 CALayer 切换到 CoreGraphics

它完成了工作。

- (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);
}
}

现在它可以在 IB 和设备/模拟器上正确呈现。

关于ios - CALayer 未在设备/模拟器上显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30890462/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com