如何将UIBarButtonItem图像添加到一个程序化创建的Tab Bar中?

问题描述 投票:0回答:1

我已经用UITabBar控制器程序化地创建了一个UITabBar。 现在,我只想让标签条独立存在,而不使用UITabBar控制器。 下面是在我的视图中确实加载的代码。

    let tabbar = UITabBar()
    tabbar.barTintColor = UIColor(red: 245/255, green: 245/255, blue: 245/255, alpha: 1.0)
    tabbar.frame = CGRect(x: 0, y: 813, width: 414, height: 49)
    self.view.addSubview(tabbar)

我不知道我这样做是否正确 但这些是我想添加到标签栏的标签按钮图像。

    let leafIcon = UIBarButtonItem(image: #imageLiteral(resourceName: "HomeLeaf"), style: .plain, target: self, action: #selector(btnMenuAction))
    leafIcon.tintColor = UIColor(red: 0/255, green: 180/255, blue: 75/255, alpha: 1.0)

    let awardIcon = UIBarButtonItem(image: #imageLiteral(resourceName: "Awards"), style: .plain, target: self, action: #selector(btnMenuAction))

    let groupIcon = UIBarButtonItem(image: #imageLiteral(resourceName: "Groups"), style: .plain, target: self, action: #selector(btnMenuAction))
    groupIcon.tintColor = UIColor(red: 0/255, green: 180/255, blue: 75/255, alpha: 1.0)

    let tabBarItems = [leafIcon,awardIcon,groupIcon]

    self.navigationItem.rightBarButtonItems = tabBarItems

当我运行这段代码时,标签栏出现了,但我不知道如何添加按钮图像。

ios swift xcode uitabbar
1个回答
0
投票

UITabbar使用 UITabbarItem 而不是 UIBarButtonItem. 基本示例 :

   var tabBarIteam = UITabBarItem()

   let selectedImage = UIImage(named: "imagename")
   let deselectedImage = UIImage(named: "imagename")

   tabBarIteam = self.tabbar.items![0] // For first ViewController
   tabBarIteam.selectedImage = selectedImage1
   tabBarIteam.image = deSelectedImage1
© www.soinside.com 2019 - 2024. All rights reserved.