6

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

Krunal
  • 77,632
  • 48
  • 245
  • 261
ricks
  • 3,154
  • 31
  • 51
  • What do you mean by *"under my UITabBarController"*? Do you mean the actual TabBar? and by "Under" do you mean Under as in behind (partially covered)? Or as in below? Show an image of what you *want* to achieve. – DonMag Nov 08 '17 at 17:09
  • Sorry i mean below the tab bar, so essentially i would move the tab bar up and display a ad below it – ricks Nov 08 '17 at 17:14
  • If on iOS 10 and below: `self.viewController.edgesForExtendedLayout |= UIRectEdgeBottom`.. If on iOS 11+: `[view.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor];` instead of `[view.bottomAnchor constraintEqualToAnchor:self.safeLayoutGuide.bottomAnchor]` Notice the difference on iOS 11.. One is safeLayoutGuide.bottom (this one will be ABOVE the tabBar) and the other is view.bottom (this one will be UNDER the tabBar). – Brandon Nov 08 '17 at 17:34

2 Answers2

15

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
Krunal
  • 77,632
  • 48
  • 245
  • 261
  • 1
    @RickS - I was typing up the same answer when Kunal posted. There is no reason why you ***cannot*** do this programmatically. – DonMag Nov 08 '17 at 19:15
1

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
        }
    }
ricks
  • 3,154
  • 31
  • 51
  • 1
    This add a view under the tab bar but this view will not responds to any events. Suppose you should add a button in it, it will not responds. – Patrick Bodet Mar 26 '18 at 07:41