[英]How to subclass a UITabBarController and replace its UITabBar view?
[英]How to remove/replace tabBar items in UITabbarController subclass in Swift?
我看不到為什么在這條線上出現錯誤thread 1 breakpoint 1.1
:
self.tabBar.setItems(items, animated: false)
我嘗試用自定義按鈕替換標簽欄中的當前項目。
這是代碼:
class MyTabController : UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let button1 = UIButton(frame: CGRectMake(0, 0, self.view.frame.width*0.3, self.view.frame.height))
button1.addTarget(self, action: "button1Clic", forControlEvents: UIControlEvents.TouchUpInside)
button1.backgroundColor = UIColor.orangeColor()
/*
let items = NSMutableArray(array: self.tabBar.items!)
items.removeObjectAtIndex(0)
*/
let array = [button1]
let items = NSMutableArray(array: array)
self.tabBar.setItems(items, animated: false) //HERE
/*
//in objective-c
NSMutableArray *items = [NSMutableArray arrayWithArray:self.tabBar.items];
[items removeObjectAtIndex:0];
[self.tabBar setItems:items];
*/
self.tabBar.addSubview(button1)
}
func button1Clic() {
println("1 clic")
}
}
編輯:
這不起作用,我得到和以前一樣的錯誤:
let tab1 = UITabBarItem(title: "test", image: UIImage(named:"food3.png"), tag: 0)
self.tabBar.setItems([tab1], animated: false)
關於按鈕,它們是否已正確添加到tabBar,但是否位於其他項目上方? 有沒有辦法通過按鈕替換uitabbarItem?
謝謝
問題是您正在使用UIButton數組調用self.tabBar.setItems
。 應該使用UITabBarItem數組調用它。
let barItem = UITabBarItem(tabBarSystemItem: .Featured, tag: 0)
self.tabBar.setItems([barItem], animated: false)
如果要在標簽欄上使用UIButton,只需在編寫時調用addSubview:
let button1 = UIButton(frame: CGRectMake(0, 0, self.view.frame.width*0.3, self.view.frame.height))
button1.addTarget(self, action: "button1Clic", forControlEvents: UIControlEvents.TouchUpInside)
button1.backgroundColor = UIColor.orangeColor()
self.tabBar.addSubview(button1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.