Mar*_*nna 4 xcode objective-c uitabbarcontroller ios swift
你好,我正在试图找出如何使用我想要的颜色tabBar.
我知道如何改变背景,我也知道如何改变tabbar.item颜色和文字.但我无法理解如何:
如果选择了项目,则更改颜色(我使用的渲染模式始终是原始的,因为我无法找到另一种方法从非选定的标签栏项目中删除默认的灰色.)
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
tabBarItem.title = "test"
tabBarItem.image = UIImage(named: "first.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
}
Run Code Online (Sandbox Code Playgroud)我怎么能在我想要的状态下使用我想要的颜色?
A UITabBar具有tintColor属性,但这会为所选图像设置色调,而不是未选择的图像.您正确设置未选择的图像AFAIK.为了改变所选图像的颜色,你可以使用tintColor的UITabBar(如果你希望所有的图像具有相同的色调),或将您UITabBarItem的selectedImage使用渲染模式AlwaysOriginal.
tabBarItem.selectedImage = UIImage(named: "first-selected")!.imageWithRenderingMode(.AlwaysOriginal)
Run Code Online (Sandbox Code Playgroud)
我已经将UIImage设置为一个未包装的可选项,因为如果没有图像文件,您可能希望它崩溃.它将有助于确保您的图像实际被加载,而不是默默地失败:-)
您可能还想设置标签的颜色,否则您的文字将与图像颜色不匹配.下面设置了所有的默认值UITabBarItem,但您可以基于每个项目设置(或覆盖)它.
UITabBarItem.appearance().setTitleTextAttributes({NSForegroundColorAttributeName: UIColor.blueColor()}, forState:.Selected)
UITabBarItem.appearance().setTitleTextAttributes({NSForegroundColorAttributeName: UIColor.redColor()}, forState:.Normal)
Run Code Online (Sandbox Code Playgroud)
这是你如何快速 3 / 4
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blue], for: .selected)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .normal)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8743 次 |
| 最近记录: |