Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在超阿巴斯背景中设置渐变_Iphone_Objective C_Ios_Ipad_Uitabbar - Fatal编程技术网

Iphone 在超阿巴斯背景中设置渐变

Iphone 在超阿巴斯背景中设置渐变,iphone,objective-c,ios,ipad,uitabbar,Iphone,Objective C,Ios,Ipad,Uitabbar,我能实现以下目标的最简单方法是什么: 它是通过对uitabarcontroller子类化实现的吗?如果是,需要更改哪些属性 @interface UITabBarController (private) - (UITabBar *)tabBar; @end @implementation CustomizeUITabBarController - (void)viewDidLoad { [super viewDidLoad]; CGRect frame = CGRect

我能实现以下目标的最简单方法是什么:

它是通过对uitabarcontroller子类化实现的吗?如果是,需要更改哪些属性

@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

希望它能帮助您……

是的,子类化UITabbarController是正确的方法

覆盖drawRect方法:

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

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

这是一个老问题,但在搜索TabBar渐变时首先出现。要做到这一点,一个简单得多的方法是通过外表。只需将其放在ApplicationIDFinishLaunching方法中

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

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

我的应用程序也有同样的问题,我提出了以下建议: 在ApplicationIDFinishLaunching方法中,我创建了一个函数来绘制渐变,并使用我的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;
}
获取uitabarcontroller的实例

UITabBarController *tabVC = (UITabBarController *)[UIApplication sharedApplication].windows.firstObject.rootViewController;
设置你的梯度

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

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

这取决于你想在哪里展示这一点。。。请发布更多详细信息。来自UITabBarController类参考:“该类不用于子类化。”如果您这样做,您可能会遇到获得应用程序批准的问题。
[UITabBar appearance].backgroundImage = [self drawGradientInView:tabVC];