Subview appears underneath superviews layer.border?

前端 未结 4 1325
情歌与酒
情歌与酒 2020-12-01 09:02

I have a UIView in which I define it\'s border in the following manner:

self.layer.borderColor = [UIColor blackColor].CGColor;
self.layer.border         


        
相关标签:
4条回答
  • 2020-12-01 09:32

    Depending on your view structure, it might be easier to add the subview to the parent of your main view. It can then overlap the main view and will overlay the border as you requested.

    0 讨论(0)
  • 2020-12-01 09:35

    Did you try setting the superview's 'clipsToBounds' property to YES? This is set to NO by default for performance reasons, but setting it to yes might give you the effect you are looking for.

    0 讨论(0)
  • 2020-12-01 09:49

    Insert layer at specific position that suits you:

    self.layer.insertSublayer(sublayer, at: 0)
    
    0 讨论(0)
  • 2020-12-01 09:52

    According to the Apple specification: It is composited above the receiver’s contents and sublayers.

    So, the border will always be above of all your subviews, even if you bring your subview to the front and so on.

    So I make a background view to fake the border.

    E.g.:

    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    backgroundView.backgroundColor = [UIColor blackColor];
    backgroundView.clipsToBounds = NO;
    
    UIView *bView = [[UIView alloc] initWithFrame:CGRectInset(backgroundView.bounds, 3, 3)];
    bView.backgroundColor = [UIColor redColor];
    
    UIView *cView = [[UIView alloc] initWithFrame:CGRectMake(-50, -50, 100, 100)];
    cView.backgroundColor = [UIColor yellowColor];
    [bView addSubview:cView];
    
    [backgroundView addSubview:bView];
    
    [self.window addSubview:backgroundView];
    

    and the effect:

    enter image description here

    0 讨论(0)
提交回复
热议问题