2017-12-28 152 views
6

używam tabBarController stworzyć program muzyczny i mam pytania jak jak to zrobić, jak pokazano na gifTabBarController dla programu muzycznego

pytania:

  1. Jak zrobić tak, że kiedy kliknij na tabBarItem, "presentViewController" pracował

  2. Jak to zrobić, aby zdjęcie nie zmieniło koloru i nie zaokrągliło się, tylko w trzecim tabBarItem

    Korzystnie bez bibliotek

enter image description here

powinno być

enter image description here

Moja TabBarController

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) 
     } 
    } 
} 

gracza

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 
    } 
} 

Odpowiedz

1

TabBarController nie ma tych opcji, trzeba wdrożyć go przez podklasy.

Możesz użyć tej biblioteki Animated Tab Bar, aby uzyskać ten sam efekt z animacją.

+0

nie chcę ikon animacji – Vasya2014

1

utworzonego widoku i przycisk na nim w TabBarController i stworzył func, które ustawia parametry i nazwać go po tym, jak ustawić wszystkie kontrolery zobacz także stworzył notificationCenter które mogą zmienić obraz przycisku z innego kontrolera

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) 
    } 
} 

} 

więc zmienić obraz przycisku

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