iOS:如何将阴影添加到`tabbar`?

时间:2016-06-27 03:01:06

标签: ios shadow tabbar

我有一个测试,为我的tabbar添加阴影,但它没有用。我的代码如下:

    _button.layer.shadowColor = [UIColor grayColor].CGColor;
    _button.layer.shadowOffset = CGSizeMake(4,4);
    _button.layer.shadowOpacity = 0.8;
    _button.layer.shadowRadius = 4;

    /* remove tab-line */
    [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
    [self.tabBarController.tabBar setClipsToBounds:YES];

    self.tabBarController.tabBar.layer.shadowColor = [UIColor grayColor].CGColor;
    self.tabBarController.tabBar.layer.shadowOffset = CGSizeMake(0,-4);
    self.tabBarController.tabBar.layer.shadowOpacity = 0.8;
   self.tabBarController.tabBar.layer.shadowRadius = 4;

我还拍了一张照片来表明:

the button has shadow but tabbar has nothing

1 个答案:

答案 0 :(得分:2)

使用阴影创建图像,然后使用如下。

[[UITabBar appearance] setBackgroundImage:[[UIImage alloc] INITWITHYOURCUSTOMIMAGE]]; 
[[UITabBar appearance] setShadowImage:[[UIImage alloc] INITWITHYOURCUSTOMIMAGE]];

在swift:

UITabBar.appearance().shadowImage = UIImage(named:"YOURIMAGE")
UITabBar.appearance().backgroundImage = UIImage(named:"YOURIMAGE")