Cocoa Touch: How To Change UIView's Border Color And Thickness?

后端 未结 14 1059
心在旅途
心在旅途 2020-11-30 16:56

I saw in the inspector that I can change the background color, but I\'d like to also change the border color and thickness, is this possible?

14条回答
  •  醉梦人生
    2020-11-30 17:19

    When I use Vladimir's CALayer solution, and on top of the view I have an animation, like a modal UINavigationController dismissing, I see a lot of glitches happening and having drawing performance issues.

    So, another way to achieve this, but without the glitches and performance loss, is to make a custom UIView and implement the drawRect message like so:

    - (void)drawRect:(CGRect)rect
    {
        CGContextRef contextRef = UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(contextRef, 1);
        CGContextSetRGBStrokeColor(contextRef, 255.0, 255.0, 255.0, 1.0);
        CGContextStrokeRect(contextRef, rect);    
    }
    

提交回复
热议问题