Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TabBarController for music program

I use tabBarController to create a music program and I have questions like how to do it as shown in gif

Questions:

  1. How to do so that when you click on tabBarItem, "presentViewController" worked

  2. How to make it so that the photo does not change color and make it round, only in the third tabBarItem

    Preferably without libraries

enter image description here

it should be

enter image description here

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

Player

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
    }
 }
like image 727
Vasya2014 Avatar asked Dec 28 '17 12:12

Vasya2014


2 Answers

The TabBarController doesn't have those option, you need to implement it by subclassing.

You can use this library Animated Tab Bar to achieve the same result with animation.

like image 93
Arasuvel Avatar answered Oct 26 '22 01:10

Arasuvel


I created a view and a button on it in tabBarController and created a func that sets the parameters and call it after I set all the view controllers also created a notificationCenter that could change the button image from another controller

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

}

so I change the image of the button

    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "imageChange"), object: nil)
like image 39
Vasya2014 Avatar answered Oct 26 '22 00:10

Vasya2014