Set the CALayer 'borderWidth' and 'cor

2019-07-03 15:37发布

问题:

- (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;
}

Effect:

Border not fully cover the the background.

Setting shadowxxx has no effect.

回答1:

This is the intended behaviour of layer's property. If you look at Apple Documentation for borderWidth property, you will find: -

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.

If you are in need to fill the borderWidth portion with blackColor as well, then you have two choices

  1. There is no point in having a border whose color is not different than the content of view. You will not be able to see the border at all. All you will see is a little bigger circle filled with blackColor.
  2. If you wanna keep border and also needs to fill it with black color then assign the borderColor property to blackColor.


标签: ios calayer