单击时如何将标签栏项目分配给特定的视图控制器?

时间:2017-09-18 15:24:59

标签: ios swift xcode

import UIKit
import ARNTransitionAnimator
import AVFoundation
import MediaPlayer

final class ViewController: UIViewController, AVAudioPlayerDelegate{

@IBOutlet fileprivate(set) weak var containerView : UIView!
@IBOutlet fileprivate(set) weak var tabBar : UITabBar!
@IBOutlet fileprivate(set) weak var miniPlayerView : LineView!
@IBOutlet fileprivate(set) weak var miniPlayerButton : UIButton!    
@IBOutlet  weak var miniPlay: UIButton!

@IBOutlet weak var tabone: UITabBarItem!

private var animator : ARNTransitionAnimator?
fileprivate var modalVC : ModalViewController!


override func viewDidLoad() {
    super.viewDidLoad()

    tabBar.barTintColor = UIColor.black

    let musicVC = MusicViewController()


    }
}

我正在尝试将标签栏项目分配给视图控制器。当前的视图控制器不是一个特殊的标签控制器。(我知道在故事板中制作标签栏控制器会更容易)。标签栏是在故事板中制作的对象,并且由IBoutlet连接,其第一个条形项也连接在一起。有没有办法以编程方式分配连接?

1 个答案:

答案 0 :(得分:0)

我建议UIViewController上的扩展程序将标签栏项属性复制到它的UITabBarItem实例。

extension UIViewController {
    func copyTabBarItem(_ item: UITabBarItem) {
        tabBarItem.badgeColor = item.badgeColor
        tabBarItem.badgeValue = item.badgeValue
        if #available(iOS 11.0, *) {
            tabBarItem.isSpringLoaded = item.isSpringLoaded
        }
        tabBarItem.selectedImage = item.selectedImage
        for state in [UIControlState.application, UIControlState.disabled, UIControlState.focused, UIControlState.highlighted, UIControlState.normal, UIControlState.reserved, UIControlState.selected] {
            tabBarItem.setBadgeTextAttributes(item.badgeTextAttributes(for: state), for: state)
        }
        tabBarItem.titlePositionAdjustment = item.titlePositionAdjustment
    }
}