如何更改标签栏项目的默认灰色? [英] How to change default grey color of tab bar items?
问题描述
我尝试更改 Tab Bar
项目的默认灰色,但 Xcode 发现错误.我使用了一些代码,该代码是:
I tried to change default grey color of Tab Bar
items, but Xcode finds error. I used some code, that code is:
import UIKit
extension UIImage {
func makeImageWithColorAndSize(color: UIColor, size: CGSize) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(CGRectMake(0, 0, size.width, size.height))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
class SecondViewController: UIViewController {
let tabBar = UITabBar()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
UITabBar.appearance().selectionIndicatorImage = UIImage().makeImageWithColorAndSize(UIColor.blueColor(), size: CGSizeMake(tabBar.frame.width/CGFloat(tabBar.items!.count), tabBar.frame.height))
}
所以我把它放在 SecondViewController
中作为测试,当我在 Xcode Simulator 上运行应用程序时它崩溃并在日志中显示错误(控制台)致命错误:解包时意外发现 nil一个可选值
So I put this in SecondViewController
just as test, and when I run application on Xcode Simulator it crash and it show error in logs (console) fatal error: unexpectedly found nil while unwrapping an Optional value
我认为问题出在这里:
UITabBar.appearance().selectionIndicatorImage = UIImage().makeImageWithColorAndSize(UIColor.blueColor(), size: CGSizeMake(tabBar.frame.width/CGFloat(tabBar.items!.count), tabBar.frame.height))
因为当我删除这部分代码时,不会发生错误.有人可以帮我吗?
Because when I delete this part of code, error doesn't happen. Can someone help me ?
推荐答案
你创建 UITabBar
对象的代码中的问题,比如 let tabBar = UITabBar()
和这个对象与位于表单上的选项卡无关.你的 tabBar
是一个新的空对象,它不包含任何 UITabBarItem
对象,当你调用它时:
The problem in your code that you create UITabBar
object like let tabBar = UITabBar()
and this object has no relation to the tabs which are located on the form. Your tabBar
is a new empty object that contains no one UITabBarItem
objects and when you call this:
UITabBar.appearance().selectionIndicatorImage = UIImage().makeImageWithColorAndSize(UIColor.blueColor(), size: CGSizeMake(tabBar.frame.width/CGFloat(tabBar.items!.count), tabBar.frame.height))
当您尝试执行此操作时发生错误:tabBar.items!.count
.您正在尝试解开可选的 items
数组 [UITabBarItem]?
并且它 nil
因为 tabBar
是空对象并且没有物品.
the error occurs when you try to do this: tabBar.items!.count
. You're trying to unwrap optional items
array [UITabBarItem]?
and it nil
becouse tabBar
is empty object and have no items.
要解决此问题,您需要从当前 UITabBarController
获取对 UITabBar
的引用,例如:
To fix this you need to get reference to UITabBar
from current UITabBarController
for example like this:
class SecondViewController: UIViewController {
var tabBar: UITabBar?
override func viewDidLoad() {
super.viewDidLoad()
tabBar = self.tabBarController!.tabBar
tabBar!.selectionIndicatorImage = UIImage().makeImageWithColorAndSize(UIColor.blueColor(), size: CGSizeMake(tabBar!.frame.width/CGFloat(tabBar!.items!.count), tabBar!.frame.height))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这篇关于如何更改标签栏项目的默认灰色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!