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

时间:2015-09-16 20:37:25

标签: ios swift autolayout iad nslayoutconstraint

tableView下面有iAd bannertableView

目标

tableViews无法接收广告时,我试图让banner身高变大。当它可以获得广告时,tableViews高度应该缩小。 (如果可能,我想有动画。)

问题

当我运行应用程序时,banner最初被隐藏(请参阅下面的代码),然后是 tableViews高度就像是banner一样。意思是底部有空间。然后,当出现banner时,tableViews高度变得更小,并且它有2 banners的空间。

(如果这不清楚请告诉我,我会发布图片。)

以下是约束条件:

的UITableView:
enter image description here

iAd Banner:
enter image description here

以下是代码:

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

1 个答案:

答案 0 :(得分:1)

为您的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
}

就是这样!