2017-12-28 9 views
6

のために、私は音楽プログラムを作成するためにtabBarControllerを使用して、私はGIFにTabBarControllerが音楽番組

質問示すようにそれを行う方法のような質問がありますように、あなたを行うにはどのよう

  1. をtabBarItemをクリックすると、 "presentViewController"が処理されます

  2. 写真が色を変えずに丸くなるようにする方法は、ライブラリ

enter image description here

無し好ましくは

それは私のTabBarController

enter image description here

でなければなりません

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.delegate = self 

    // меняет цвет фона tabBar 
    self.tabBar.barTintColor = .white 

    // меняет цвет UITabBarItem and Title 
    UITabBar.appearance().tintColor = UIColor(hex: 0x0077fe, alpha: 1) 

    //меняет цвет background UITabBar 
    UITabBar.appearance().barTintColor = UIColor.white 


    // делает фон серым 
    for item in self.tabBar.items! { 
     if let image = item.image { 
      item.image = image.withRenderingMode(.alwaysOriginal) 

     } 
    } 

    //показывает и переходит в контроллеры 
    let storyBoard = UIStoryboard(name: "Main", bundle:nil) 
    let controller1 = storyBoard.instantiateViewController(withIdentifier: "main") as! VCMain 
    let controller2 = storyBoard.instantiateViewController(withIdentifier: "search") 
    let controller3 = storyBoard.instantiateViewController(withIdentifier: "player") 
    let controller4 = storyBoard.instantiateViewController(withIdentifier: "bookmark") 
    let controller5 = storyBoard.instantiateViewController(withIdentifier: "menu") 

    self.setViewControllers([controller1,controller2,controller3,controller4,controller5], animated: true) 

    // создает навигационный контроллер для контроллеров 
    let vc1 = UINavigationController(rootViewController: controller1) 
    let vc2 = UINavigationController(rootViewController: controller2) 
    let vc3 = UINavigationController(rootViewController: controller3) 
    let vc4 = UINavigationController(rootViewController: controller4) 
    let vc5 = UINavigationController(rootViewController: controller5) 

    viewControllers = [vc1, vc2, vc3, vc4, vc5] 

} 

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) { 

    print(item.tag) 
    if item.tag == 0{ 
     if GlobalModals.count != 0 { 
      let storyboard = UIStoryboard(name: "Main", bundle: nil) 
      let vc = storyboard.instantiateViewController(withIdentifier: "player") as? VCPlayer 
      self.present(vc!, animated: true, completion: nil) 
     } 
    } 
} 

プレーヤー

override func viewDidLoad() { 
    super.viewDidLoad() 

     let im = Extension.resizeImage(image:GlobalModals[thisSong].ImageView! , targetSize: CGSize.init(width:20, height: 20)) 
     self.tabBarController?.tabBar.items![2].image = im 
    } 
} 

答えて

1

TabBarControllerは、これらのオプションを持っていませんが、あなたはサブクラスでそれを実装する必要があります。

このライブラリAnimated Tab Barを使用すると、アニメーションで同じ結果を得ることができます。

+0

ボタンの画像を変更します – Vasya2014

1

私はtabBarControllerで、その上に表示し、ボタンを作成し、パラメータを設定し、私もすべてのビューコントローラを設定する別のコントローラからのボタン画像を変更することができます通知センターを作成した後、それを呼び出すFUNCを作成

class TabBarController: UITabBarController,UITabBarControllerDelegate { 


open var playerBtn = UIButton() 

//func for NotificationCenter 
@objc func imageChange() { 
    if GlobalModals.count != 0 { 
     playerBtn.setImage(GlobalModals[thisSong].ImageView, for: .normal) 
    } 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    NotificationCenter.default.addObserver(self, selector: #selector(imageChange), name: NSNotification.Name(rawValue: "imageChange"), object: nil) 

    self.delegate = self 

    //shows and goes to controllers 
    let storyBoard = UIStoryboard(name: "Main", bundle:nil) 
    let controller1 = storyBoard.instantiateViewController(withIdentifier: "main") as! VCMain 
    let controller2 = storyBoard.instantiateViewController(withIdentifier: "search") 
    let controller3 = storyBoard.instantiateViewController(withIdentifier: "player") 
    let controller4 = storyBoard.instantiateViewController(withIdentifier: "bookmark") 
    let controller5 = storyBoard.instantiateViewController(withIdentifier: "menu") 

    self.setViewControllers([controller1,controller2,controller4,controller5], animated: true) 

    // creates navigation controller 
    let vc1 = UINavigationController(rootViewController: controller1) 
    let vc2 = UINavigationController(rootViewController: controller2) 
    let vc3 = UINavigationController(rootViewController: controller3) 
    let vc4 = UINavigationController(rootViewController: controller4) 
    let vc5 = UINavigationController(rootViewController: controller5) 

    viewControllers = [vc1, vc2, vc3, vc4, vc5] 

    self.setupMiddleButton() 



} 

// TabBarButton – Setup Middle Button and View 
func setupMiddleButton() { 
    playerBtn.frame = CGRect(x: 0, y: 0, width: 45, height: 45) 
    var playerBtnFrame = playerBtn.frame 
    playerBtnFrame.origin.y = self.view.bounds.height - playerBtnFrame.height - 2 
    playerBtnFrame.origin.x = self.view.bounds.width/2 - playerBtnFrame.size.width/2 
    playerBtn.frame = playerBtnFrame 
    playerBtn.layer.cornerRadius = playerBtnFrame.height/2 
    playerBtn.layer.masksToBounds = true 
    playerBtn.contentMode = .scaleAspectFill 


    let view = UIView() 
    let width = self.view.frame.width/5 
    let xWidth = width*2 
    view.frame = CGRect(x: xWidth , y: playerBtnFrame.origin.y - 2, width: self.view.frame.width/5, height: tabBar.frame.height) 
    view.backgroundColor = .clear 
    self.view.addSubview(view) 
    self.view.addSubview(playerBtn) 

    playerBtn.setImage(UIImage(named: "home"), for: UIControlState.normal) 
    playerBtn.addTarget(self, action: #selector(playerButtonAction), for: UIControlEvents.touchUpInside) 

    self.view.layoutIfNeeded() 
} 

// Button Touch Action 
@objc func playerButtonAction(sender: UIButton) { 
    if GlobalModals.count != 0 { 
     let storyboard = UIStoryboard(name: "Main", bundle: nil) 
     let vc = storyboard.instantiateViewController(withIdentifier: "player") as? VCPlayer 
     self.present(vc!, animated: true, completion: nil) 
    } 
} 

} 

はので、私はアニメーションアイコンを望んでいない

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "imageChange"), object: nil)