首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IOS 8选项卡栏项目背景颜色

IOS 8选项卡栏项目背景颜色
EN

Stack Overflow用户
提问于 2015-05-05 06:31:34
回答 5查看 34K关注 0票数 19

在过去的一周里,我一直在努力寻找这个问题的解决方案,但在尝试了我能找到或想到的所有可能的解决方案后,我一直没有找到任何幸运的答案。我找到并尝试的每一个解决方案要么不起作用,要么已经过时了。

我将5个UITabBarItem放在UITabBarController中的UITabBar中。我想要在UITabBarItem被选中时更改它的背景颜色,当然,当所选项目更改时,它也会变回来。

我使用的是Swift和Xcode6.3.1中的iOS SDK8.3。如果你只能用Objective-C来回答,那也没问题,任何答案都会有帮助!提前感谢大家,我真的很感谢!

编辑:这是一个我想要它做什么的可视化示例。

Different Background Color

EN

回答 5

Stack Overflow用户

发布于 2015-07-28 23:28:50

在你的tabBarController中,你可以设置图片的默认UITabBar tintColor,barTintColor,selectionIndicatorImage (这里有点作弊)和renderingMode,请参见下面的评论:

代码语言:javascript
复制
    class MyTabBarController: UITabBarController, UINavigationControllerDelegate {
      ...
      override func viewDidLoad() {
        ...
        // Sets the default color of the icon of the selected UITabBarItem and Title
        UITabBar.appearance().tintColor = UIColor.redColor()

        // Sets the default color of the background of the UITabBar
        UITabBar.appearance().barTintColor = UIColor.blackColor()

        // Sets the background color of the selected UITabBarItem (using and plain colored UIImage with the width = 1/5 of the tabBar (if you have 5 items) and the height of the tabBar)
        UITabBar.appearance().selectionIndicatorImage = UIImage().makeImageWithColorAndSize(UIColor.blueColor(), size: CGSizeMake(tabBar.frame.width/5, tabBar.frame.height))

        // Uses the original colors for your images, so they aren't not rendered as grey automatically.
        for item in self.tabBar.items as! [UITabBarItem] {
          if let image = item.image {
            item.image = image.imageWithRenderingMode(.AlwaysOriginal)
          }
        }
      }
      ...
    }

您还需要扩展UIImage类,以生成所需大小的纯色图像:

代码语言:javascript
复制
extension UIImage {
  func makeImageWithColorAndSize(color: UIColor, size: CGSize) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(size, false, 0)
    color.setFill()
    UIRectFill(CGRectMake(0, 0, size.width, size.height))
    var image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
  }
}
票数 66
EN

Stack Overflow用户

发布于 2015-05-05 10:21:35

你可以试试这个。在AppDelegate.swift中添加此代码。

代码语言:javascript
复制
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        UITabBar.appearance().translucent = false
        UITabBar.appearance().barTintColor = UIColor(rgba: "#12296f")
        UITabBar.appearance().tintColor = UIColor.whiteColor()

        return true
    }

别忘了包括这个库。https://github.com/yeahdongcn/UIColor-Hex-Swift

票数 20
EN

Stack Overflow用户

发布于 2016-03-11 22:53:52

受Gwendle的启发,我是这样解决这个问题的:

代码语言:javascript
复制
override func viewWillAppear(animated: Bool) {
    guard let tabBar = tabBarController?.tabBar else { return }
    tabBar.tintColor = UIColor.whiteColor()
    tabBar.selectionIndicatorImage = UIImage().makeImageWithColorAndSize(UIColor.redColor(), size: CGSizeMake(tabBar.frame.width/5, tabBar.frame.height))
    super.viewWillAppear(animated)
}

还使用了扩展名:

代码语言:javascript
复制
extension UIImage {
    func makeImageWithColorAndSize(color: UIColor, size: CGSize) -> UIImage {
        UIGraphicsBeginImageContext(size)
        color.setFill()
        UIRectFill(CGRectMake(0, 0, size.width, size.height))
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
}

请记住,在您设置了selectionIndicationImage之后,它仍然适用于您的所有其他选项卡。下面是一个如何删除它的示例,方法是在其余选项卡中的每个其他视图控制器中将其设置为nil:

代码语言:javascript
复制
override func viewWillAppear(animated: Bool) {
    tabBarController?.tabBar.tintColor = UIColor.redColor()
    tabBarController?.tabBar.selectionIndicatorImage = nil
    super.viewWillAppear(animated)
}

使用Swift 2实现。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30041127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档