Search code examples
iosswiftxcodeuitabbar

How to access tabbar height from other controller in swift?


I want to attach a view from a specific controller (MyController) just above the tabbar but my problem is how would I calculate the height of the tabbar from MyController so that I could give y position to my custom view. I am trying to achieve with statusBarFrame but it's not working. If anybody has some idea please help me out.

let timerView = UINib(nibName: "WorkoutTimer", bundle: nil).instantiate(withOwner: nil, options: nil).first as? WorkoutTimer
    let window = UIApplication.shared.windows.last
    if let timerView = timerView {
        let screenSize = UIScreen.main.bounds
        let screenHeight = screenSize.height
        let screenWidth = screenSize.width
        let statusbarHeight = UIApplication.shared.statusBarFrame.height
        let yPosition = screenHeight - ((statusbarHeight) + 50)
        timerView.frame = CGRect(x: 0, y: yPosition, width: screenWidth, height: 50)
        window?.addSubview(timerView)
    }

Solution

  • directly take the y position of tabBarController?.tabBar.frame.origin.y and add your timerview frame height

     if let timerView = timerView {
             let yPosition = self.tabBarController?.tabBar.frame.origin.y - 80
            timerView.frame = CGRect(x: 0, y: yPosition, width: UIScreen.main.bounds.size.width, height: 50)
            window?.addSubview(timerView)
        }