Ios UIButton顶部和底部边框附加线
我正在尝试向uibutton添加顶部和底部边框。添加边框的函数如下所示Ios UIButton顶部和底部边框附加线,ios,objective-c,xcode,uibutton,Ios,Objective C,Xcode,Uibutton,我正在尝试向uibutton添加顶部和底部边框。添加边框的函数如下所示 CALayer *topBorder = [CALayer layer]; topBorder.frame = CGRectMake(os, 1.0f, b.bounds.size.width - (os * 2.0f), 1.0f); topBorder.backgroundColor = [c1 CGColor]; [b.layer addSublayer:topBorder];
CALayer *topBorder = [CALayer layer];
topBorder.frame = CGRectMake(os, 1.0f, b.bounds.size.width - (os * 2.0f), 1.0f);
topBorder.backgroundColor = [c1 CGColor];
[b.layer addSublayer:topBorder];
CALayer *bottomBorder = [CALayer layer];
bottomBorder.frame = CGRectMake(os, b.bounds.size.height, b.bounds.size.width - (os * 2.0f), 1.0f);
bottomBorder.backgroundColor = [c1 CGColor];
[b.layer addSublayer:bottomBorder];
//os..offset, b..uibutton, c1..color
当我在ViewDidDisplay中调用该函数时,它工作得很好,但是有一个延迟,但是当我将它放在viewdidlayoutsubviews中时,它会添加一个额外的行,不知怎的看起来是这样的
我将其设置为superview的前导和尾随空格,我在这里做错了什么?当视图出现在屏幕上时,以及每次从导航流中的其他视图控制器返回时,都会执行ViewDidDisplay。因此,您的代码多次添加行。您可以查看视图控制器生命周期
如果要删除延迟并执行一次,请在viewDidLoad中编写代码
更新
我认为有两种方法。创建UIButton子类,然后:
如果你想继续使用CALayers,你需要调用一个redraw方法来改变按钮的大小。如果更改为动画,则会出现奇怪的工件
好的。我建议您使用“drawRect”绘制这些线,并使用按钮框动态计算线XY。如果你改变按钮的大小,不管是否设置动画…没问题,核心动画完成所有工作
- (void)drawRect:(CGRect)rect
{
// Frames
CGRect frame = self.bounds;
// Parameters (change these values if you want to change appearance)
UIColor *lineColor = [UIColor colorWithRed: 0.5 green: 0.5 blue: 0.5 alpha: 1];
CGFloat topRatioPositionY = 0.02;
CGFloat middleRatioPositionY = 0.6;
CGFloat bottomRatioPositionY = 0.98;
CGFloat middleRatioPositionX = 0.33333;
CGFloat lineWidth = 1.0;
//// Top line
UIBezierPath* bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint: CGPointMake(CGRectGetMinX(frame), CGRectGetMinY(frame) + topRatioPositionY * CGRectGetHeight(frame))];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 1.00000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + topRatioPositionY * CGRectGetHeight(frame))];
[lineColor setStroke];
bezierPath.lineWidth = lineWidth;
[bezierPath stroke];
// Middle line
UIBezierPath* bezier2Path = [UIBezierPath bezierPath];
[bezier2Path moveToPoint: CGPointMake(CGRectGetMinX(frame), CGRectGetMinY(frame) + bottomRatioPositionY * CGRectGetHeight(frame))];
[bezier2Path addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 1.00000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + bottomRatioPositionY * CGRectGetHeight(frame))];
[lineColor setStroke];
bezier2Path.lineWidth = lineWidth;
[bezier2Path stroke];
// Bottom Line
UIBezierPath* bezier3Path = [UIBezierPath bezierPath];
[bezier3Path moveToPoint: CGPointMake(CGRectGetMinX(frame), CGRectGetMinY(frame) + middleRatioPositionY * CGRectGetHeight(frame))];
[bezier3Path addLineToPoint: CGPointMake(CGRectGetMinX(frame) + middleRatioPositionX * CGRectGetWidth(frame), CGRectGetMinY(frame) + middleRatioPositionY * CGRectGetHeight(frame))];
[lineColor setStroke];
bezier3Path.lineWidth = lineWidth;
[bezier3Path stroke];
}// drawRect:
我用CALayer和UIBezierPath解决方案附加了这一行。触按按钮并查看它们变化时的差异。这就是我将其放入ViewDidLayout子视图的原因,因为在该阶段,它知道按钮将有多宽,viewDidLoad中的情况并非如此,而是viewDidLoad中有真正的根视图框。它允许您使用多种方法来解决它。但是我建议您忘记CALayer,使用drawRect。我编辑了我的答案。嘿torcelly,谢谢你的支持,我不知道这是否只是我的一些奇怪的设置,但是drawRect方法对xcode项目中按钮内标题前面的这个工件也有相同的作用,但不是calayer解决方案,我可能会使用后面的解决方案……谢谢