Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 276
ricks Avatar asked Nov 08 '17 17:11

ricks


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
like image 86
Krunal Avatar answered Sep 21 '22 02:09

Krunal


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
        }
    }
like image 29
ricks Avatar answered Sep 19 '22 02:09

ricks