隐藏另一个视图时调整视图大小

Resize view when another view is hidden

我在 tableView 下面有一个 tableView 和一个 iAd banner

目标

banner无法接收广告时,我正在尝试使tableViews高度变大。当它可以获取广告时,tableViews 高度应该缩小。 (如果可以的话,我想有动画。)

问题

当我 运行 应用程序时 banner 最初是隐藏的(见下面的代码),并且 tableViews身高好像有一个banner。这意味着它的底部有 space。然后当banner出现的时候,tableViews的高度就变得更小了space for 2 banners.

(如果不清楚,请告诉我,我会 post 一张图片。)

限制条件如下:

UITableView:

iAd 横幅:

代码如下:

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

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

更新

我尝试将 height constraint 添加到 ad banner,当我 运行 应用程序时,出现以下错误:

 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

为您的 iAd 横幅添加高度限制并使其成为 ViewController 中的出口。并更改您的功能:

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
}

就是这样!