【发布时间】:2015-11-07 19:03:02
【问题描述】:
我尝试更改 Tab Bar 项目的默认灰色,但 Xcode 发现错误。我使用了一些代码,该代码是:
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强>
我认为问题出在这里:
UITabBar.appearance().selectionIndicatorImage = UIImage().makeImageWithColorAndSize(UIColor.blueColor(), size: CGSizeMake(tabBar.frame.width/CGFloat(tabBar.items!.count), tabBar.frame.height))
因为当我删除这部分代码时,不会发生错误。 有人可以帮我吗?
【问题讨论】:
标签: xcode swift uitabbarcontroller