添加一个 UITabBarItem 到一个已经存在的 UITabBar(在故事板中制作)
Add a UITabBarItem to an already existing UITabBar (made in storyboard)
因此,我需要向在故事板中创建的现有 UITabBar
添加一个新的 UITabBarItem
。
到目前为止,我认为我已经能够适当地访问 UITabBar
,但无法创建 UITabBarItem
并将其添加到 UITabBar
。
到目前为止,我得到的是:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
var tabBarController = self.window!.rootViewController as UITabBarController
var tabBar = tabBarController.tabBar as UITabBar
var tabBarItem1 = tabBar.items![0] as UITabBarItem
var tabBarItem2 = tabBar.items![1] as UITabBarItem
var tabBarItem3 = tabBar.items![2] as UITabBarItem
tabBarItem1.selectedImage = UIImage(named: "tab-frame-on")
tabBarItem2.selectedImage = UIImage(named: "tab-quote-on")
tabBarItem3.selectedImage = UIImage(named: "tab-hash-on")
var instaTab: UITabBarItem = UITabBarItem(title: "", image: UIImage(named: "tab-instagram-off"), selectedImage: UIImage(named: "tab-instagram-on"))
tabBar.items?.append(instaTab)
return true
}
此函数是 AppDelegate
的一部分。正如您在第 14 行看到的那样,我正在创建 UITabBarItem
,如下所示:var instaTab: UITabBarItem = UITabBarItem(title: "", image: UIImage(named: "tab-instagram-off"), selectedImage: UIImage(named: "tab-instagram-on"))
,然后尝试通过附加它来将其添加到预先存在的选项卡栏项目中。这让我在 AppDelegate
class 实例化时出现 SIGABRT 错误。
我做错了什么?
如果您查看 Xcode 控制台,它应该会告诉您哪里做错了。
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Directly modifying a tab bar managed by a tab bar controller is not allowed.'
tabBar.items
由UITabBarController
控制。您必须改为修改 UITabBarController 的 viewControllers
属性。
例如:
tabBarItem3.selectedImage = UIImage(named: "tab-hash-on")
let instaViewController = UIViewController()
instaViewController.tabBarItem = UITabBarItem(title: "", image: UIImage(named: "tab-instagram-off"), selectedImage: UIImage(named: "tab-instagram-on"))
var viewControllers = tabBarController.viewControllers!
viewControllers.append(instaViewController)
tabBarController.viewControllers = viewControllers
return true
因此,我需要向在故事板中创建的现有 UITabBar
添加一个新的 UITabBarItem
。
到目前为止,我认为我已经能够适当地访问 UITabBar
,但无法创建 UITabBarItem
并将其添加到 UITabBar
。
到目前为止,我得到的是:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
var tabBarController = self.window!.rootViewController as UITabBarController
var tabBar = tabBarController.tabBar as UITabBar
var tabBarItem1 = tabBar.items![0] as UITabBarItem
var tabBarItem2 = tabBar.items![1] as UITabBarItem
var tabBarItem3 = tabBar.items![2] as UITabBarItem
tabBarItem1.selectedImage = UIImage(named: "tab-frame-on")
tabBarItem2.selectedImage = UIImage(named: "tab-quote-on")
tabBarItem3.selectedImage = UIImage(named: "tab-hash-on")
var instaTab: UITabBarItem = UITabBarItem(title: "", image: UIImage(named: "tab-instagram-off"), selectedImage: UIImage(named: "tab-instagram-on"))
tabBar.items?.append(instaTab)
return true
}
此函数是 AppDelegate
的一部分。正如您在第 14 行看到的那样,我正在创建 UITabBarItem
,如下所示:var instaTab: UITabBarItem = UITabBarItem(title: "", image: UIImage(named: "tab-instagram-off"), selectedImage: UIImage(named: "tab-instagram-on"))
,然后尝试通过附加它来将其添加到预先存在的选项卡栏项目中。这让我在 AppDelegate
class 实例化时出现 SIGABRT 错误。
我做错了什么?
如果您查看 Xcode 控制台,它应该会告诉您哪里做错了。
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Directly modifying a tab bar managed by a tab bar controller is not allowed.'
tabBar.items
由UITabBarController
控制。您必须改为修改 UITabBarController 的 viewControllers
属性。
例如:
tabBarItem3.selectedImage = UIImage(named: "tab-hash-on")
let instaViewController = UIViewController()
instaViewController.tabBarItem = UITabBarItem(title: "", image: UIImage(named: "tab-instagram-off"), selectedImage: UIImage(named: "tab-instagram-on"))
var viewControllers = tabBarController.viewControllers!
viewControllers.append(instaViewController)
tabBarController.viewControllers = viewControllers
return true