How to create UINavigationBar drop shadow

后端 未结 4 1748
臣服心动
臣服心动 2020-12-22 21:09

Would like to know create drop shadow for UINavigationbar. I tried to create custom navigation bar background with drop shadow, but the drop shadow cover the background view

4条回答
  •  被撕碎了的回忆
    2020-12-22 21:54

    If you apply a drop shadow to a UINavigationBar, the shadow is clipped at below the corners:

    clipped shadow

    This is just how shadows behave on rectangles. I usually create a path for the shadow that's a bit wider than the actual nav bar, which creates an effect that is more like what you'd generally expect:

    @implementation UINavigationBar (DropShadow)
    
    -(void)willMoveToWindow:(UIWindow *)newWindow {
        [super willMoveToWindow:newWindow];
        self.layer.shadowColor = [UIColor blackColor].CGColor;
        self.layer.shadowOpacity = 1;
        self.layer.shadowOffset = CGSizeMake(0,4);
        CGRect shadowPath = CGRectMake(self.layer.bounds.origin.x - 10, self.layer.bounds.size.height - 6, self.layer.bounds.size.width + 20, 5);
        self.layer.shadowPath = [UIBezierPath bezierPathWithRect:shadowPath].CGPath;
        self.layer.shouldRasterize = YES;
    }
    

    better

提交回复
热议问题