iosswiftstoryboard

UITabBarController Head and Body Layout


Maybe there isn't a way. I'm pretty sure there is a way to customize the UITabBarController layout to work.

My question is about having a header and body in UITabBarController. I have the following code in the TabBarLayout.

TabBarLayout.swift

import UIKit

class TabBarLayout: UITabBarController {
  
  @IBOutlet var viewHeader: UIView!

  override func viewDidLoad() {
    super.viewDidLoad()
    viewHeader.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(viewHeader)
    let leftSide = NSLayoutConstraint(item: viewHeader!, attribute: .left, relatedBy: .equal, toItem: view, attribute: .left, multiplier: 1.0, constant: 0.0)
    let rightSide = NSLayoutConstraint(item: viewHeader!, attribute: .right, relatedBy: .equal, toItem: view, attribute: .right, multiplier: 1.0, constant: 0.0)
    let topSide = NSLayoutConstraint(item: viewHeader!, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1.0, constant: 0.0)
    let height = NSLayoutConstraint(item: viewHeader!, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100)
    view.addConstraint(leftSide)
    view.addConstraint(rightSide)
    view.addConstraint(topSide)
    view.addConstraint(height)
  }
}

TabBar.storyboard

The Storyboard has UITabBarController with Storyboard References.

UITabBarController Layout

Buy.storyboard

The Buy Storyboard shows the following screenshot:

Buy Storyboard

BuyController.swift

import UIKit

class BuyController: UIViewController {
  
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    print("BUYCONTROLLER")
    let tbc = tabBarController as! TabBarLayout
    
    // How do I align this view's topAnchor to viewHeader's bottom anchor?
  }
}

How should I align the Buy Storyboard top anchor to viewHeader's bottom anchor so that the Test Label is visible?

Result of iOS Simulator

iOS Simulator


Solution

  • Try to adjust the additionalSafeAreaInsets in viewWillAppear of BuyController.swift. This ensures that the view respects the space taken up by viewHeader.

    BuyController.swift:

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        let tbc = tabBarController as! TabBarLayout
        
        let headerHeight = tbc.viewHeader.frame.height
        self.additionalSafeAreaInsets.top = headerHeight
    }
    

    This method dynamically sets additionalSafeAreaInsets.top based on viewHeader’s height. So BuyController’s content starts below viewHeader, preventing overlap.