how can I add attached drop shadows to uinavigationbar and uitoolbar

风格不统一 提交于 2019-12-02 21:29:49

In the end I decided to just use background png images with the shadows, and apply them with a subclass for UINavigationBar and UIToolbar which implemented the drawRect method (for the background image) and the sizeThatFits method to resize the navigation bar. Here is the final product (the button hides the bars):

Here are the methods I implemented in each subclass:

 - (void)drawRect:(CGRect)rect {
   UIImage *image = [[UIImage imageNamed:@"bargloss-withshadow.png"] retain];
   [image drawInRect:rect];
   [image release];
}


- (CGSize)sizeThatFits:(CGSize)size {
    CGSize newSize = CGSizeMake(320,60);
    return newSize;
}

Please note that I also made the bars Black Translucent in IB so that the content flowed under them.

It can be done relatively easy assuming you are not doing anything too fancy with your view hierarchy. Add the following lines of code the your application:didFinishLaunchingWithOptions method.

dispatch_async(dispatch_get_main_queue(), ^{
    UIWindow* mainWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    UIView* mainView = [[mainWindow subviews] objectAtIndex:0];
    UIImageView* shadowImageView = [[UIImageView alloc] initWithImage:kImgNavbarShadowResizeable];
    shadowImageView.frame = CGRectMake(0, 64, 320, shadowImageView.frame.size.width);
    [mainView insertSubview:shadowImageView atIndex:1];
});

The shadowImageView is the imageView you want to use as the shadow.

Check out Matt Gallagher's blog post. It covers (part of) what you want to do. Other than that, you can try adding a "shadow view" above your navigation controller which you can animate as you wish.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!