I tried to change the gray color of the default elements Tab Bar, but Xcode detected an error. I used some code, this code:
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()
UITabBar.appearance().selectionIndicatorImage = UIImage().makeImageWithColorAndSize(UIColor.blueColor(), size: CGSizeMake(tabBar.frame.width/CGFloat(tabBar.items!.count), tabBar.frame.height))
}
So, I put this in SecondViewControllerexactly the same way as the test, and when I run the application on Xcode Simulator, it crashes and it shows an error in logs (console) fatal error: nil was unexpectedly found when deploying an optional value
I think the problem is here:
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 the code, no error occurs. Can anybody help me?
source
share