How to add only a TOP border on a UIButton?

后端 未结 11 1701
自闭症患者
自闭症患者 2020-12-13 03:45

I know how to add border to a button in iOS 7, with the following code :

[[myButton layer] setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5]          


        
11条回答
  •  攒了一身酷
    2020-12-13 04:22

    Actually I meet this questions as you,but I think my method is better than the answer which you choose. You should create a class inherit the UIControl like UIButton.

    @interface customButton : UIButton
    

    and rewrite the drawrect method like this:

    - (void)drawRect:(CGRect)rect {
        // Drawing code
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetLineCap(context, kCGLineCapRound);
        CGContextSetLineWidth(context, 1.5);  //线宽
        CGContextSetAllowsAntialiasing(context, true);
        CGContextSetRGBStrokeColor(context, 193/255.0, 205/255.0, 193/255.0, 1.0);  //线的颜色
        CGContextBeginPath(context);
    
        CGContextMoveToPoint(context, 0, 0);  //起点坐标
        CGContextAddLineToPoint(context, self.frame.size.width, 0);   //终点坐标
    
        CGContextStrokePath(context);
    }
    

    By the way~ your purpose UIControl should use your class in xib's setting

    [![this setting][1]][1]

    Last~ show you my custom UIButton. I think we should choose this method and combine UIBezierPath's API to finish our demand.

    [![enter image description here][2]][2]

    Thank you for watching~ hope to study and discuss together~ From a iOS fisherman -- vvlong [1]: https://i.stack.imgur.com/ywIZk.png [2]: https://i.stack.imgur.com/mmfOB.png

提交回复
热议问题