Dee*_*kor 160 xcode uitabbaritem uitabbar ios xcode6
我想在选中时将标签栏项目更改为粉红色,而不是默认的蓝色.
如何使用Xcode 6中的storyboard编辑器完成此操作?
这是我目前的设置不起作用,蓝色背景有效,但粉红色不起作用:

Meh*_*kar 392
从StoryBoard添加名为"tintColor"的运行时颜色属性.这是有效的(对于Xcode 8及更高版本).
如果你想要未选择的颜色..你也可以添加unselectedItemTintColor.

Mar*_*tti 158
这个优雅的解决方案适用于SWIFT 3.0和SWIFT 4.0:
在故事板上:
编辑:使用Xcode 8测试,适用于iOS 10及更高版本
Jar*_*rod 58
在Swift中,使用xcode 7(及更高版本),您可以将以下内容添加到AppDelegate.swift文件中:
UITabBar.appearance().tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
Run Code Online (Sandbox Code Playgroud)
这就是完整方法的样子:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// I added this line
UITabBar.appearance().tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
return true
}
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,我的项目将为白色.需要"/255.0",因为它需要从0到1的值.对于白色,我可能刚刚使用1.但对于其他颜色,您可能会使用RGB值.
Der*_*ike 16
斯威夫特3 | Xcode 10
如果要使所有标签栏项目具有相同的颜色(选定和未选定)...
步骤1
确保将图像资源设置为Render As = Template Image.这允许他们继承颜色.
第2步
使用情节提要编辑器更改标签栏设置,如下所示:
第3步
步骤1和2将更改所选图标的颜色.如果您仍想更改未选定项目的颜色,则需要在代码中执行此操作.我还没有找到通过故事板编辑器来做到这一点的方法.
创建自定义标签栏控制器类...
// TabBarController.swift
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
// make unselected icons white
self.tabBar.unselectedItemTintColor = UIColor.white
}
}
Run Code Online (Sandbox Code Playgroud)
...并将自定义类指定给标签栏场景控制器.
如果您想通过故事板编辑器找出如何更改未选择的图标颜色,请告诉我.谢谢!
小智 9
将此代码放在要更改颜色的视图控制器的viewDidLoad中
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
Run Code Online (Sandbox Code Playgroud)
小智 7
XCode 8.2,iOS 10,Swift 3:现在有一个unselectedItemTintColor属性tabBar:
self.tabBar.unselectedItemTintColor = UIColor(red: 0/255.0, green: 200/255.0, blue: 0/255.0, alpha: 1.0)
Run Code Online (Sandbox Code Playgroud)
您可以通过storyboard更改颜色UITabBarItem,但如果您想通过代码更改颜色,则非常容易:
//用于更改所选栏的颜色
[[UITabBar appearance] setTintColor:[UIColor blueColor]];
Run Code Online (Sandbox Code Playgroud)
//这个用于更改未选中的栏(iOS 10)
[[UITabBar appearance] setUnselectedItemTintColor:[UIColor yellowColor]];
Run Code Online (Sandbox Code Playgroud)
//此行用于更改所有tabbar的颜色
[[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
Run Code Online (Sandbox Code Playgroud)
不知何故,我们无法单独使用故事板更改Tab Bar选择的项目Tint颜色,因此我在ViewDidLoad中添加了下面的代码,希望这会有所帮助.
[[UITabBar appearance] setTintColor:[UIColor whiteColor]];
Run Code Online (Sandbox Code Playgroud)
这是 Swift 3 中适用于 iOS 10 的解决方案:
首先,创建自己的选项卡栏控制器子类并将其添加到情节提要中的选项卡控制器中。在该viewDidLoad()方法中,您可以自定义选项卡栏。这里需要说明的是, 的tintColor属性tabBar代表的是选中项的颜色,而不是未选中项的颜色!为了更改未选定项目的颜色,我建议循环遍历每个项目并使用图像的原始颜色,这样它们就不会自动渲染为灰色。
class CustomTabBarVC: UITabBarController
{
override func viewDidLoad()
{
super.viewDidLoad()
self.tabBar.tintColor = AppColor.normalRed
self.tabBar.barTintColor = .white
self.tabBar.isTranslucent = true
if let items = self.tabBar.items
{
for item in items
{
if let image = item.image
{
item.image = image.withRenderingMode( .alwaysOriginal )
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这种方法的唯一缺点是您的商品图像必须已经具有您想要的所需颜色。
将此代码添加到您的应用程序委托中-did_finish_launching_with_options函数
UITabBar.appearance().tintColor = UIColor( red: CGFloat(255/255.0), green: CGFloat(99/255.0), blue: CGFloat(95/255.0), alpha: CGFloat(1.0) )
Run Code Online (Sandbox Code Playgroud)
放置所需颜色的RGB
| 归档时间: |
|
| 查看次数: |
141828 次 |
| 最近记录: |