ricks
ricks

Reputation: 3324

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

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

Upvotes: 6

Views: 9306

Answers (2)

ricks
ricks

Reputation: 3324

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

Upvotes: 1

Krunal
Krunal

Reputation: 79646

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

Upvotes: 15

Related Questions