Search code examples
iosswiftautolayoutiadnslayoutconstraint

Resize view when another view is hidden


I have a tableView and a iAd banner beneath the tableView.

Goal

I'm trying to make the tableViews height get larger when the banner is unable to receive ads. And when it can get ads, the tableViews height should shrink. (I would like to have animation if possible.)

Problem

When I run the app the banner is hidden initially (see code below), and the tableViews height is as if there's a banner. Meaning it has the space on the bottom. Then when the banner appears, the tableViews height becomes even smaller and it has the space for 2 banners.

(If this isn't clear please let me know and I'll post an image.)

Here's the constraints:

UITableView:
enter image description here

iAd Banner:
enter image description here

Here's the code:

func bannerViewDidLoadAd(banner: ADBannerView!) {
    self.bottomAddView?.hidden = false
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
    self.bottomAddView?.hidden = true
}

Update

I tried adding a height constraint to the ad banner, and when I run the app, I get the following error:

 Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<myAPp.MainViewController 0x7f9bd1d213b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key bottomAddView.'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000011048ac65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000110123bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000011048a8a9 -[NSException raise] + 9
    3   Foundation                          0x000000010fcb9b53 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259
    4   CoreFoundation                      0x00000001103d2d50 -[NSArray makeObjectsPerformSelector:] + 224
    5   UIKit                               0x0000000110d0752b -[UINib instantiateWithOwner:options:] + 1506
    6   UIKit                               0x0000000110b5f718 -[UIViewController _loadViewFromNibNamed:bundle:] + 242
    7   UIKit                               0x0000000110b5fd08 -[UIViewController loadView] + 109
    8   UIKit                               0x0000000110b5ff79 -[UIViewController loadViewIfRequired] + 75
    9   UIKit                               0x0000000110b9001b -[UINavigationController _layoutViewController:] + 44
    10  UIKit                               0x0000000110b90565 -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 216
    11  UIKit                               0x0000000110b90664 -[UINavigationController _startTransition:fromViewController:toViewController:] + 92
    12  UIKit                               0x0000000110b91448 -[UINavigationController _startDeferredTransitionIfNeeded:] + 523
    13  UIKit                               0x0000000110b91f0e -[UINavigationController __viewWillLayoutSubviews] + 43
    14  UIKit                               0x0000000110cdc715 -[UILayoutContainerView layoutSubviews] + 202
    15  UIKit                               0x0000000110aafa2b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536
    16  QuartzCore                          0x000000011275dec2 -[CALayer layoutSublayers] + 146
    17  QuartzCore                          0x00000001127526d6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    18  QuartzCore                          0x0000000112752546 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    19  QuartzCore                          0x00000001126be886 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
    20  QuartzCore                          0x00000001126bfa3a _ZN2CA11Transaction6commitEv + 462
    21  UIKit                               0x0000000110a2da2d -[UIApplication _reportMainSceneUpdateFinished:] + 44
    22  UIKit                               0x0000000110a2e6f1 -[UIApplication _runWithMainScene:transitionContext:completion:] + 2648
    23  UIKit                               0x0000000110a2d0d5 -[UIApplication workspaceDidEndTransaction:] + 179
    24  FrontBoardServices                  0x000000011771d5e5 __31-[FBSSerialQueue performAsync:]_block_invoke_2 + 21
    25  CoreFoundation                      0x00000001103be41c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    26  CoreFoundation                      0x00000001103b4165 __CFRunLoopDoBlocks + 341
    27  CoreFoundation                      0x00000001103b3947 __CFRunLoopRun + 887
    28  CoreFoundation                      0x00000001103b3366 CFRunLoopRunSpecific + 470
    29  UIKit                               0x0000000110a2cb42 -[UIApplication _run] + 413
    30  UIKit                               0x0000000110a2f900 UIApplicationMain + 1282
    31  myApp                             0x000000010f9b3207 main + 135
    32  libdyld.dylib                       0x0000000112f82145 start + 1
    33  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Solution

  • Add a height constraint to your iAd banner and make it's outlet in your ViewController. And change your functions:

    func bannerViewDidLoadAd(banner: ADBannerView!) {
        self.bottomAddView?.hidden = false
        self.yourBannerHeightConstraint.constant = someValue
    }
    
    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        self.bottomAddView?.hidden = true
        self.yourBannerHeightConstraint.constant = 0
    }
    

    And that's it!