在iOS中设置CALayer'borderWidth'和'cornerRadius',不能完全覆盖背景

Set the CALayer 'borderWidth' and 'cornerRadius' in iOS, can not fully covers background

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.yuanjiao.backgroundColor = [UIColor blackColor];
    self.yuanjiao.layer.cornerRadius = self.yuanjiao.frame.size.width/2;
    self.yuanjiao.layer.masksToBounds = YES;
    self.yuanjiao.layer.borderWidth = 5;
    self.yuanjiao.layer.borderColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1].CGColor;

    //    self.yuanjiao.layer.shadowOffset = CGSizeMake(0, 0);
    //    self.yuanjiao.layer.shadowRadius = 0.0;
    //    self.yuanjiao.layer.shadowColor = [UIColor whiteColor].CGColor;
    //    self.yuanjiao.layer.shadowOpacity = 0.0;
}

效果:

边框没有完全覆盖背景。

设置 shadowxxx 无效。

这是图层 属性 的预期行为。如果你查看 Apple Documentation for borderWidth 属性,你会发现:-

Discussion
When this value is greater than 0.0, the layer draws a border using the current borderColor value. The border is drawn inset from the receiver’s bounds by the value specified in this property. It is composited above the receiver’s contents and sublayers and includes the effects of the cornerRadius property.

The default value of this property is 0.0.

如果您还需要用 blackColor 填充 borderWidth 部分,那么您有两个选择

  1. 具有与视图内容颜色相同的边框是没有意义的。您将根本看不到边界。您将看到的只是一个更大的圆圈,里面填满了 blackColor.
  2. 如果你想保留边框并且还需要用黑色填充它然后将 borderColor 属性 分配给 blackColor.