Мой UItabbarController работает нормально на симуляторе, но всякий раз, когда я запускаю его на устройстве, он получает cra sh при просмотре изображений UItabbarItem, Imageview показывает ноль. Если я прокомментирую утверждение «если позволено», оно всегда вылетает при первом firstTabbarItemImageView.content = .center и т. Д. Для каждого другого изображения, когда он находит nil
, что мне делать, чтобы справиться с ситуацией?
Код My TabbarController
class TabBarController: UITabBarController {
enum TabbarItemTag: Int {
case firstViewController = 101
case secondViewConroller = 102
case thirdViewController = 103
case forthViewController = 104
case fifthViewController = 105
}
var firstTabbarItemImageView: UIImageView!
var secondTabbarItemImageView: UIImageView!
var thirdTabbarItemImageView: UIImageView!
var forthTabbarItemImageView: UIImageView!
var fifthTabbarItemImageView: UIImageView!
@IBOutlet weak var tabbarOutlet : UITabBar!
override func viewDidLoad() {
super.viewDidLoad()
let firstItemView = tabbarOutlet.subviews.first!
firstTabbarItemImageView = firstItemView.subviews.first as? UIImageView
if let firstTab = firstTabbarItemImageView {
firstTab.contentMode = .center
}
let secondItemView = self.tabbarOutlet.subviews[1]
self.secondTabbarItemImageView = secondItemView.subviews.first as? UIImageView
if let secondTabbar = secondTabbarItemImageView {
secondTabbar.contentMode = .center
}
let thirdItemView = self.tabbarOutlet.subviews[2]
self.thirdTabbarItemImageView = thirdItemView.subviews.first as? UIImageView
if let thirdTabbar = thirdTabbarItemImageView {
thirdTabbar.contentMode = .center
}
let forthItemView = self.tabbarOutlet.subviews[3]
self.forthTabbarItemImageView = forthItemView.subviews.first as? UIImageView
if let forthTabbar = forthTabbarItemImageView {
forthTabbar.contentMode = .center
}
let fifthItemView = self.tabbarOutlet.subviews[4]
self.fifthTabbarItemImageView = fifthItemView.subviews.first as? UIImageView
if let fifthTabbar = fifthTabbarItemImageView {
fifthTabbar.contentMode = .center
}
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
}
override func viewDidAppear(_ animated: Bool) {
self.navigationController?.interactivePopGestureRecognizer?.delegate = self
}
private func animate(_ imageView: UIImageView) {
UIView.animate(withDuration: 0.1, animations: {
imageView.transform = CGAffineTransform(scaleX: 1.25, y: 1.25)
}) { _ in
UIView.animate(withDuration: 0.25, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 3.0, options: .transitionFlipFromTop, animations: {
imageView.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
}, completion: nil)
}
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
guard let tabbarItemTag = TabbarItemTag(rawValue: item.tag) else {
return
}
switch tabbarItemTag {
case .firstViewController:
animate(firstTabbarItemImageView)
case .secondViewConroller:
animate(secondTabbarItemImageView)
case .thirdViewController:
animate(thirdTabbarItemImageView)
case .forthViewController:
animate(forthTabbarItemImageView)
case .fifthViewController:
animate(fifthTabbarItemImageView)
}
}
}
При запуске на симуляторе
Когда работает на iphone