如何将附加阴影添加到 uinavigationbar 和 uitoolbar

发布于 2024-10-31 08:20:21 字数 315 浏览 7 评论 0原文

我正在开发一个为 UINavigationBar 和 UIToolbar 使用自定义图像的应用程序,这很好,但它们还需要导航栏下方和工具栏上方的阴影,该阴影始终位于所有其他视图控制器之上。我不能简单地制作包含阴影的背景图像,因为这不适用于我的表格视图。当我将导航栏和工具栏设置为隐藏(动画)时,我还需要动画消失阴影。

我浏览了 Stack Overflow 和网上的其他资源,并研究了建议的解决方案,但我无法成功子类 UINavigationBar、UIToolbar 甚至 UINavigationController 来提供我想要的结果。

将不胜感激可以提供的任何帮助。谢谢 :)

I'm working on an app that uses a custom image for UINavigationBar and UIToolbar, which is fine, but they also need a drop shadow below the nav bar and above the toolbar, which would always rest above all other view controllers. I cannot simply make background images which include the shadows as this would not work well with my tableviews. I also need the shadows animate away when I set nav bar and tool bar to hidden (animated).

I've looked through Stack Overflow and other sources on the net, and have worked through the proposed solutions but I cannot successfully subclass UINavigationBar, UIToolbar or even UINavigationController to provide the results I am after.

Would appreciate any help that could be offered. Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最终幸福 2024-11-07 08:20:21

最后,我决定只使用带有阴影的背景 png 图像,并将它们应用到 UINavigationBar 和 UIToolbar 的子类中,该子类实现了 drawRect 方法(用于背景图像)和 sizeThatFits 方法来调整导航栏的大小。这是最终产品(按钮隐藏了栏):

以下是我在每个子类中实现的方法:

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

请注意我还在 IB 中将条形设置为黑色半透明,以便内容在它们下方流动。

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.

风吹过旳痕迹 2024-11-07 08:20:21

假设您没有对视图层次结构做任何过于花哨的事情,那么这可以相对容易地完成。将以下代码行添加到您的应用程序:didFinishLaunchingWithOptions 方法中。

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];
});

ShadowImageView 是要用作阴影的 imageView。

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.

差↓一点笑了 2024-11-07 08:20:21

请查看 Matt Gallagher 的博客文章。它涵盖了您想做的(部分)事情。除此之外,您可以尝试在导航控制器上方添加一个“阴影视图”,您可以根据需要为其设置动画。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文