How can i add a UIView under my tab bar controller programatically?

Viewed 8380

I am trying to see how i can add a UIView under my UITabBarController so i can add ads to my app, I cant seem to figure out any way to constrain my UIView to the bottom of the tab bar. Is this possible?

EDIT: By bottom of the tab bar i mean below the tab bar

2 Answers

Try this add see:

enter image description here

Follow these steps to achieve it:

  1. Add UIViewController in root of your storyboard
  2. Add Container View inside UIViewController
  3. Add AdView below Container view
  4. Embed UITabbarController with Container view

I was able to create a UIView in my UITabBarController

lazy var bannerAd: UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.backgroundColor = .black
    return view
}()

And then pin it to the bottom like so:

  view.addSubview(bannerAd)

    bannerAd.heightAnchor.constraint(equalToConstant: 44).isActive = true
    bannerAd.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
    bannerAd.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true

then to move up the Tab Bar i did so like this:

override func viewWillLayoutSubviews() {
        if !didStyleTabBar {
            self.tabBar.invalidateIntrinsicContentSize()
            var tabFrame = self.tabBar.frame

            tabFrame.size.height = tabBarHeight
            tabFrame.origin.y = tabFrame.origin.y - 44
            self.tabBar.frame = tabFrame

            didStyleTabBar = true
        }
    }
Related