在tabBar中添加了一个空白控制器,外边覆盖了一个按钮.但是按钮添加之后不显示.后来发现应该把代码写在
viewDidLayoutSubviews方法里边,而不是viewDidLoad里边.
class MainViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
addChildViewControllers()
}
private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
tabBar.tintColor = UIColor.orangeColor()
vc.title = title
vc.tabBarItem.image = UIImage(named: imageName)
let nav = UINavigationController(rootViewController: vc)
addChildViewController(nav)
}
private func addChildViewControllers() {
addChildViewController(HomeTableViewController(), title: "首页", imageName: "")
addChildViewController(MessageTableViewController(), title: "消息", imageName: "")
addChildViewController(UIViewController())
addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "")
addChildViewController(ProfileTableViewController(), title: "我", imageName: "")
}
private lazy var composeButton: UIButton = {
let btn = UIButton()
btn.setImage(UIImage(named: "cmp_inf_select"), forState: UIControlState.Normal)
btn.addTarget(self, action: "clickComposeButton", forControlEvents: UIControlEvents.TouchUpInside)
self.tabBar.addSubview(btn)
return btn
}()
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
setUpComposeButton()
}
private func setUpComposeButton() {
let w = self.tabBar.bounds.width / CGFloat(childViewControllers.count)
let rect = CGRectMake(0, 0, w, self.tabBar.bounds.height)
composeButton.frame = CGRectOffset(rect, 2 * w, 0)
}
func clickComposeButton() {
print("clickComposeButton")
}