在 UITabBar 背景中设置渐变

发布于 2024-12-03 11:06:19 字数 147 浏览 1 评论 0原文

我可以实现这样的事情的最简单方法是什么:

在此处输入图像描述

是通过子类化 UITabBarController 吗?如果是的话需要改变什么属性?

What is the easiest way that I can achieve something like this:

enter image description here

Is it through subclassing a UITabBarController? If yes what property needs to be changed?

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

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

发布评论

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

评论(4

反目相谮 2024-12-10 11:06:19
@interface UITabBarController (private)
- (UITabBar *)tabBar;
@end

@implementation CustomizeUITabBarController


- (void)viewDidLoad {
    [super viewDidLoad];

    CGRect frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 48);
    UIView *v = [[UIView alloc] initWithFrame:frame];
    [v setBackgroundColor:kMainColor];
    [v setAlpha:0.5];
    [[self tabBar] addSubview:v];
    [v release];

}
@end

希望它能帮助你......

@interface UITabBarController (private)
- (UITabBar *)tabBar;
@end

@implementation CustomizeUITabBarController


- (void)viewDidLoad {
    [super viewDidLoad];

    CGRect frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 48);
    UIView *v = [[UIView alloc] initWithFrame:frame];
    [v setBackgroundColor:kMainColor];
    [v setAlpha:0.5];
    [[self tabBar] addSubview:v];
    [v release];

}
@end

Hope it will help you....

做个ˇ局外人 2024-12-10 11:06:19

是的,子类化 UITabbarController 是执行此操作的正确方法。

覆盖drawRect方法:

- (void)drawRect:(CGRect)rect {
    [[UIImage imageNamed:@"yourNavigationBar.png"] drawInRect: CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) ];
}

并将带有渐变的图像添加到您的项目中。

Yes, subclassing an UITabbarController is the right way to do this.

Overwrite the drawRect method:

- (void)drawRect:(CGRect)rect {
    [[UIImage imageNamed:@"yourNavigationBar.png"] drawInRect: CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) ];
}

And add the image with gradient into your project.

凯凯我们等你回来 2024-12-10 11:06:19

这是一个老问题,但它在搜索 TabBar 渐变时首先出现。一个更简单的方法是通过外观来做到这一点。只需将其放入您的 applicationDidFinishLaunching 方法中即可。

[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"navBarImage_1x49"]];

而且,作为奖励,如果您喜欢在代码中创建渐变图像,您可以这样做:

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = CGRectMake(0, 0, 1, 49);
gradient.colors = [NSArray arrayWithObjects:(id)[UIColor.darkGrayColor] CGColor], (id)[UIColor.blackColor] CGColor], nil];
UIGraphicsBeginImageContext(gradient.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[gradient renderInContext:context];
UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

This is an old question but it comes up first in a search for TabBar gradients. A far easier way to do this is with appearances. Just put this in your applicationDidFinishLaunching method.

[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"navBarImage_1x49"]];

And, as a bonus, if you prefer to create a gradient image in code, you can do this:

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = CGRectMake(0, 0, 1, 49);
gradient.colors = [NSArray arrayWithObjects:(id)[UIColor.darkGrayColor] CGColor], (id)[UIColor.blackColor] CGColor], nil];
UIGraphicsBeginImageContext(gradient.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[gradient renderInContext:context];
UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
来日方长 2024-12-10 11:06:19

我的应用程序遇到了同样的问题,我想出了以下方法:
在 applicationDidFinishLaunching 方法中,我创建了一个绘制渐变的函数,并使用 UITabBarController 的实例根据设备的宽度设置正确的渐变框架。

- (UIImage *)drawGradientInView:(UITabBarController *) tabBarVC {
    CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = CGRectMake(CGRectGetMinX(tabBarVC.tabBar.frame), CGRectGetMinY(tabBarVC.tabBar.frame), CGRectGetWidth(tabBarVC.view.frame), CGRectGetHeight(tabBarVC.tabBar.frame));

    //set up your gradient
    //......

    UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return gradientImage;
}

获取 UITabBarController 的实例

UITabBarController *tabVC = (UITabBarController *)[UIApplication sharedApplication].windows.firstObject.rootViewController;

设置渐变

[UITabBar appearance].backgroundImage = [self drawGradientInView:tabVC];

我不确定这是否是正确的方法,但它确实对我有用。

I had the same problem with my app and I came up with following:
in applicationDidFinishLaunching method I created a function to draw a gradient and used an instance of my UITabBarController to set up a correct gradient frame depending on the width of the device.

- (UIImage *)drawGradientInView:(UITabBarController *) tabBarVC {
    CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = CGRectMake(CGRectGetMinX(tabBarVC.tabBar.frame), CGRectGetMinY(tabBarVC.tabBar.frame), CGRectGetWidth(tabBarVC.view.frame), CGRectGetHeight(tabBarVC.tabBar.frame));

    //set up your gradient
    //......

    UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return gradientImage;
}

get the instance of UITabBarController

UITabBarController *tabVC = (UITabBarController *)[UIApplication sharedApplication].windows.firstObject.rootViewController;

set your gradient

[UITabBar appearance].backgroundImage = [self drawGradientInView:tabVC];

I'm not sure if that's a correct approach but it did work for me.

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