gpt4 book ai didi

swift 3 : UITabBarController error: '*** -[__NSArray0 objectAtIndex:]: index 1 beyond bounds for empty NSArray'

转载 作者:行者123 更新时间:2023-11-28 15:45:22 24 4
gpt4 key购买 nike

我已经创建了一个自定义标签栏 Controller 。我已经通过 Storyboard 完成了这个并且工作正常。我现在正试图通过代码实现这一点,没有 Storyboard ,但我一直因为这个错误而崩溃。

'*** -[__NSArray0 objectAtIndex:]: index 1 beyond bounds for empty NSArray'

有人知道问题出在哪里吗?谢谢大家...

import UIKit


class SecondController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = .white
}
}

class ThirdController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = .white
}
}

class AnimatedTabBarController: UITabBarController {

var secondItemImageView: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()

let secondItemView = self.tabBar.subviews[1]
self.secondItemImageView = secondItemView.subviews.first as! UIImageView
self.secondItemImageView.contentMode = .center

}

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {

if item.tag == 1 {
//do our animations
self.secondItemImageView.transform = CGAffineTransform.identity

UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1, options: .curveEaseInOut, animations: { () -> Void in

let rotation = CGAffineTransform(rotationAngle: CGFloat(M_PI_2))
self.secondItemImageView.transform = rotation

}, completion: nil)

}

}

}

class CustomTabBarController: AnimatedTabBarController {

override func viewDidLoad() {
super.viewDidLoad()

let firstController = ThirdController()
let firstNavController = UINavigationController(rootViewController: firstController)
firstNavController.title = "Requests"
firstNavController.tabBarItem.image = UIImage(named: "home_selected")
firstNavController.tabBarItem.tag = 0

let secondController = SecondController()
let secondNavController = UINavigationController(rootViewController: secondController)
secondNavController.title = "Messenger"
secondNavController.tabBarItem.image = UIImage(named: "home_selected")
secondController.tabBarItem.tag = 1

viewControllers = [firstNavController, secondNavController]

tabBar.isTranslucent = false

let topBorder = CALayer()
topBorder.frame = CGRect(x: 0, y: 0, width: 1000, height: 0.5)
topBorder.backgroundColor = UIColor.lightGray.cgColor

tabBar.layer.addSublayer(topBorder)
tabBar.clipsToBounds = true

}
}

最佳答案

我注意到一件事:

let secondItemView = self.tabBar.subviews[1]

AnimatedTabBarController.viewDidLoad()的第一行。但是,选项卡栏 Controller 直到 正确 之后才在 CustomTabBarController.viewDidLoad() 中设置。我认为您会希望在 init 方法中初始化包含的 View Controller 。

关于 swift 3 : UITabBarController error: '*** -[__NSArray0 objectAtIndex:]: index 1 beyond bounds for empty NSArray' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43163320/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com