UIButton的顶部和底部边框附加线

问题描述:

我想一个顶部和底部边框添加到一个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 *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 

这个时候我调用函数中viewDidAppear(正常工作但有一个延迟则),但是当我把它放在viewdidlayoutsubviews它增加了一个额外的线,什么莫名其妙看起来像这样 enter image description here

我设置了一个开头和结尾的空间,它的父,有什么ai的做错了什么?

viewDidAppear当您查看时会在屏幕上显示并且每次从您的导航流程中的其他视图控制器返回时执行。所以你的代码多次添加行。您可以查看视图控制器生命周期here

如果要删除“延迟”并执行一次,请在viewDidLoad中编写代码。

UPDATE

我看到了两种方法。创建一个UIButton子类,然后:

  1. 如果你想保持与CALayers工作,你将需要调用再拉法总是你的按钮改变它的大小。如果更改动画会出现奇怪的神器。

  2. 好的一个。我建议您使用'drawRect'来绘制这些线条,并使用按钮框架动态地计算XY线。如果你按钮改变其大小,动画或不...没问题,Core Animation可以完成所有工作。

    - (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解决方案线连接这source code。触摸按钮并在改变时看到差异。

+0

这就是为什么我把它放到viewdidlayoutsubviews,因为在那个阶段它知道按钮的宽度是多少,什么不是在viewdidload中的情况 – Markus

+0

你是对的,但在'viewDidLoad'你有真正的根视图框架。它允许你用几种方法来解决它。不过,我建议你忘记CALayer并使用'drawRect'。我编辑了我的答案。 – torcelly

+0

嘿torcelly,谢谢你的负担,我不知道它是否只是我的一些奇怪的设置,但drawRect方法与按钮内部的标题前面的工件(在您的xcode项目中)相同,但不是我可能会使用后者的解决方案解决方案......谢谢 – Markus