Set CALayer to 'borderWidth' and 'cornerRadius' on iOS, can't completely cover the 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;
}

      

Effect:

i.stack.imgur.com/alPDr.png

The border layer does not completely cover the background.

The shadowxxx setting has no effect.

+3


source to share


1 answer


This is the intended behavior of the layer property. If you look at Apple Documentation for the property borderWidth

, you will find: -

Discussion
When this value is greater than 0.0, the layer draws a border using the current borderColor value. The boundary is drawn inset from the boundaries of the receivers at the value specified in this property. It is structured over content and receiver sublayers and includes the effects of the cornerRadius property.

By default, this value is 0.0.



If you need to fill a part borderWidth

blackColor

then you have two options

  • It doesn't make sense to have a border that is the same color as the content of the view. You won't be able to see the border at all. All you will see is a slightly larger circle filled in blackColor

    .
  • If you want to keep the border and also need to fill it with black, set the property borderColor

    to blackColor

    .
+2


source







All Articles