如何将UITabBarItem添加到UITabBar

时间:2017-04-25 09:02:41

标签: ios swift uitabbar

以下是我的BaseViewController

的一部分
class BaseViewController: UIViewController,MenuProtocol {

//DECLARE Control Objects
var MyTabBar:UITabBar = UITabBar()

在viewDidLoad中,我使用约束

将MyTabBar添加到子视图中

但是我无法在其上添加TabBarItems。

func MyCreateAndPlaceTabBarItems(){
    if MyTabBarStringArray.count == 0 {
        return
    }

    var count = 0

    for item in MyTabBarStringArray{

        let  tabItem = UITabBarItem(title: item, image: nil, tag: count)
        tabItem.badgeColor = .orange
        MyTabBar.items?.append(tabItem)

        count = count + 1
    }
}

enter image description here

海军蓝部分是MyTabBar。

我在这里缺少什么?

1 个答案:

答案 0 :(得分:1)

我只是运行你的代码,发现添加图像后我可以看到UITabBarItem

class ViewController: UIViewController {


@IBOutlet var MyTabBar: UITabBar!
var MyTabBarStringArray = [String]()
override func viewDidLoad() {
    super.viewDidLoad()
    MyTabBarStringArray = ["a","d","e"]
    self.MyCreateAndPlaceTabBarItems()
    // Do any additional setup after loading the view, typically from a nib.
}

func MyCreateAndPlaceTabBarItems(){
    if MyTabBarStringArray.count == 0 {
        return
    }

    var count = 0

    for item in MyTabBarStringArray{

        let  tabItem = UITabBarItem(title: item, image: UIImage.init(named: "IconPinPoint"), tag: count)
        tabItem.badgeColor = .orange
        MyTabBar.items?.append(tabItem)
        count = count + 1
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}