UINavigationbar 的阴影效果,如 GameCenter

发布于 2024-10-05 12:29:44 字数 130 浏览 8 评论 0原文

我想为 UINavigationbar 添加阴影效果,如 GameCenter。

我认为将带有阴影的背景图像应用于导航栏,但标题的行高会降低。 我将阴影绘制到背景上,但背景图像不会滚动。

这个案例的最佳实践是什么?

I want to add shadow effect for UINavigationbar like GameCenter.

I think apply background image with shadow to nav bar, but title's line height would be down.
And I draw shadow to background, but background image would not scroll.

What is the best Practice of this case??

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

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

发布评论

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

评论(3

夏有森光若流苏 2024-10-12 12:29:44

您可以子类化 UINavigationController,然后为每个导航设置一个阴影层,或者如果您的栏始终可见,只需将阴影添加到 UIWindow(整个应用程序只有一个),然后每次添加子视图时将其设为最前面的视图。

CGColorRef darkColor = [[UIColor blackColor] colorWithAlphaComponent:.5f].CGColor;
CGColorRef lightColor = [UIColor clearColor].CGColor;

CAGradientLayer *newShadow = [[[CAGradientLayer alloc] init] autorelease];
newShadow.frame = CGRectMake(0, self.navigationBar.frame.size.height, self.navigationBar.frame.size.width, 10);
newShadow.colors = [NSArray arrayWithObjects:(id)darkColor, (id)lightColor, nil];

[self.navigationBar.layer addSublayer:newShadow];

如果您选择后一种情况,则覆盖 didAddSubview 以使该层位于最前面:

CALayer *superlayer = self.shadowLayer.superlayer;
[self.shadowLayer removeFromSuperlayer];
[superlayer addSublayer:self.shadowLayer];

希望它有帮助。

You can subclass UINavigationController and then have a shadow layer for each navigation or if your bar is always visible just add the shadow to UIWindow (only one for the entire application) and then make it the frontmost view each time you add a subview.

CGColorRef darkColor = [[UIColor blackColor] colorWithAlphaComponent:.5f].CGColor;
CGColorRef lightColor = [UIColor clearColor].CGColor;

CAGradientLayer *newShadow = [[[CAGradientLayer alloc] init] autorelease];
newShadow.frame = CGRectMake(0, self.navigationBar.frame.size.height, self.navigationBar.frame.size.width, 10);
newShadow.colors = [NSArray arrayWithObjects:(id)darkColor, (id)lightColor, nil];

[self.navigationBar.layer addSublayer:newShadow];

If you choose the latter case then override the didAddSubview to make the layer the frontmost:

CALayer *superlayer = self.shadowLayer.superlayer;
[self.shadowLayer removeFromSuperlayer];
[superlayer addSublayer:self.shadowLayer];

Hope it helps.

池予 2024-10-12 12:29:44

使用 UINavigationController 的自定义子类可以轻松完成。关键是覆盖 -viewWillAppear: 并将子层添加到 UINavigationController 的视图层:

- (void)viewWillAppear:(BOOL)animated
{
    CGColorRef darkColor = [[UIColor blackColor] colorWithAlphaComponent:.5f].CGColor;
    CGColorRef lightColor = [UIColor clearColor].CGColor;

    CGFloat navigationBarBottom;
    navigationBarBottom = self.navigationBar.frame.origin.y + self.navigationBar.frame.size.height;

    CAGradientLayer *newShadow = [[[CAGradientLayer alloc] init] autorelease];
    newShadow.frame = CGRectMake(0,navigationBarBottom, self.view.frame.size.width, 10);
    newShadow.colors = [NSArray arrayWithObjects:(id)darkColor, (id)lightColor, nil];

    [self.view.layer addSublayer:newShadow];
    [super viewWillAppear:animated];
}

navigationBarBottom 负责 UINavigationBar 和状态栏。
渐变层参数的功劳归功于 Marcio。

It's easly done with custom subclass of UINavigationController. The key is to overwrite -viewWillAppear: and add sublayer to UINavigationController's view's layer:

- (void)viewWillAppear:(BOOL)animated
{
    CGColorRef darkColor = [[UIColor blackColor] colorWithAlphaComponent:.5f].CGColor;
    CGColorRef lightColor = [UIColor clearColor].CGColor;

    CGFloat navigationBarBottom;
    navigationBarBottom = self.navigationBar.frame.origin.y + self.navigationBar.frame.size.height;

    CAGradientLayer *newShadow = [[[CAGradientLayer alloc] init] autorelease];
    newShadow.frame = CGRectMake(0,navigationBarBottom, self.view.frame.size.width, 10);
    newShadow.colors = [NSArray arrayWithObjects:(id)darkColor, (id)lightColor, nil];

    [self.view.layer addSublayer:newShadow];
    [super viewWillAppear:animated];
}

navigationBarBottom accounts for both UINavigationBar and status bar.
Credit for gradient layer parameters goes to marcio.

究竟谁懂我的在乎 2024-10-12 12:29:44

绘制 CAGradientLayer 会产生非常均匀的阴影,但 - 在我看来 - 看起来相当不自然的阴影。

这是基于问题 带有阴影的 UIView 的答案的替代方法。

它使用 CALayer 的各种阴影属性来提供阴影效果:

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationController.navigationBar.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.navigationController.navigationBar.layer.shadowOffset = CGSizeMake(0.0f,0.0f);
    self.navigationController.navigationBar.layer.shadowOpacity = 1.0f;
    self.navigationController.navigationBar.layer.shadowRadius = 4.0f;
}

相同的技术适用于 tabBarControllertabBar

self.tabBarController.tabBar.layer.shadowColor = [[UIColor blackColor] CGColor];
self.tabBarController.tabBar.layer.shadowOffset = CGSizeMake(0.0f,0.0f);
self.tabBarController.tabBar.layer.shadowOpacity = 1.0f;
self.tabBarController.tabBar.layer.shadowRadius = 4.0f;

Drawing a CAGradientLayer gives a very uniform, but - in my opinion - a rather unnatural looking shadow.

Here's an alternative approach based upon the answer to question UIView with shadow.

It uses the various shadow properties of CALayer to give the shadow effect:

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationController.navigationBar.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.navigationController.navigationBar.layer.shadowOffset = CGSizeMake(0.0f,0.0f);
    self.navigationController.navigationBar.layer.shadowOpacity = 1.0f;
    self.navigationController.navigationBar.layer.shadowRadius = 4.0f;
}

The same technique works with the tabBarController's tabBar;

self.tabBarController.tabBar.layer.shadowColor = [[UIColor blackColor] CGColor];
self.tabBarController.tabBar.layer.shadowOffset = CGSizeMake(0.0f,0.0f);
self.tabBarController.tabBar.layer.shadowOpacity = 1.0f;
self.tabBarController.tabBar.layer.shadowRadius = 4.0f;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文