我在检查器中看到我可以改变背景颜色,但是我想改变边框的颜色和粗细,可以吗?


当前回答

试试下面的代码:

view.layer.borderColor =  [UIColor redColor].CGColor;
view.layer.borderWidth= 2.0;
[view setClipsToBounds:YES];

其他回答

view.layer.borderWidth = 1.0
view.layer.borderColor = UIColor.lightGray.cgColor

当我使用Vladimir的CALayer解决方案时,在视图的顶部我有一个动画,像一个模态UINavigationController解散,我看到很多故障发生,并有绘图性能问题。

另一种实现这个的方法,但是没有小故障和性能损失,是创建一个自定义UIView并实现drawRect消息,像这样:

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

swift 4.2中项目的边框颜色:

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell_lastOrderId") as! Cell_lastOrder
cell.layer.borderWidth = 1
cell.layer.borderColor = UIColor.white.cgColor
cell.layer.cornerRadius = 10

如果你想在不同的边线上添加不同的边框,可能添加一个带有特定样式的子视图是一种容易想出的方法。

@IBInspectable在iOS 9和Swift 2.0上为我工作

extension UIView {

@IBInspectable var borderWidth: CGFloat {
get {
        return layer.borderWidth
    }
    set(newValue) {
        layer.borderWidth = newValue
    }
}

@IBInspectable var cornerRadius: CGFloat {
    get {
        return layer.cornerRadius
    }
    set(newValue) {
        layer.cornerRadius = newValue
    }
}

@IBInspectable var borderColor: UIColor? {
    get {
        if let color = layer.borderColor {
            return UIColor(CGColor: color)
        }
        return nil
    }
    set(newValue) {
        layer.borderColor = newValue?.CGColor
    }
}