以编程方式将UIView添加到UITableView

时间:2016-07-09 19:03:27

标签: ios swift uitableview autolayout

我的故事板中有一个UITableViewController。我试图在其底部添加Google广告横幅视图(基本上是UIView)。

我知道我不能在界面生成器中拖动UIView并将其放在UITableView的底部。因此,我选择以编程方式添加广告横幅视图。这就是我编码的:

  let bannerView = GADBannerView(adSize: GADAdSize(size: CGSize(width: 320, height: 50), flags: 0))
  bannerView.translatesAutoresizingMaskIntoConstraints = false
  view.addSubview(bannerView)

  let bannerViewPinToTheBottom = NSLayoutConstraint(item: bannerView,
                                                    attribute: .Bottom,
                                                    relatedBy: .Equal,
                                                    toItem: cate,
                                                    attribute: .Bottom,
                                                    multiplier: 1.0,
                                                    constant: 0.0)

  let bannerViewCenterY = NSLayoutConstraint(item: bannerView,
                                             attribute: .CenterY,
                                             relatedBy: .Equal,
                                             toItem: view,
                                             attribute: .CenterY,
                                             multiplier: 1.0,
                                             constant: 0.0)

  let bannerViewWidth = NSLayoutConstraint(item: bannerView,
                                           attribute: .Height,
                                           relatedBy: .Equal,
                                           toItem: nil,
                                           attribute: .NotAnAttribute,
                                           multiplier: 1.0,
                                           constant: 300.0)

  let bannerViewHeight = NSLayoutConstraint(item: bannerView,
                                            attribute: .Height,
                                            relatedBy: .Equal,
                                            toItem: nil,
                                            attribute: .NotAnAttribute,
                                            multiplier: 1.0,
                                            constant: 50.0)

  // add the constraints
  bannerView.addConstraints([bannerViewPinToTheBottom, bannerViewCenterY, bannerViewWidth, bannerViewHeight])

当我运行应用程序并访问UITableViewController时,收到以下错误消息:

2016-07-09 22:00:36.653 En Yakın[20923:1114504] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7d04cc40 GADBannerView:0x7a8a8c70.bottom == UITableView:0x7bac8e00.bottom>
    When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] to debug.
2016-07-09 22:00:36.655 En Yakın[20923:1114504] View hierarchy unprepared for constraint.
    Constraint: <NSLayoutConstraint:0x7d04cc40 GADBannerView:0x7a8a8c70.bottom == UITableView:0x7bac8e00.bottom>
    Container hierarchy: 
<GADBannerView: 0x7a8a8c70; frame = (0 0; 320 50); clipsToBounds = YES; layer = <CALayer: 0x7af35fb0>>
   | <GADAdView: 0x7af3c520; frame = (0 0; 320 50); hidden = YES; opaque = NO; autoresize = W+H; gestureRecognizers = <NSArray: 0x7af3b810>; layer = <CALayer: 0x7af3c640>>
   |    | <UIScrollView: 0x7b28a000; frame = (0 0; 0 0); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7af3c340>; layer = <CALayer: 0x7af37200>; contentOffset: {0, 0}; contentSize: {0, 0}>
   |    |    | <UIImageView: 0x7af48b50; frame = (-39 -5.5; 36 2.5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x7af48c80>> - (null)
   |    |    | <UIImageView: 0x7d04bae0; frame = (-5.5 -39; 2.5 36); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x7ae1e830>> - (null)
   |    | <UIWebView: 0x7a8d20e0; frame = (0 0; 0 0); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x7a8d21b0>>
   |    |    | <_UIWebViewScrollView: 0x7b34b600; frame = (0 0; 0 0); clipsToBounds = YES; autoresize = H; gestureRecognizers = <NSArray: 0x7af46bd0>; layer = <CALayer: 0x7af468d0>; contentOffset: {0, 0}; contentSize: {0, 0}>
   |    |    |    | <UIWebBrowserView: 0x7b298a00; frame = (0 0; 0 0); text = ''; opaque = NO; gestureRecognizers = <NSArray: 0x7af44600>; layer = <UIWebLayer: 0x7a8d30c0>>
   |    |    |    |    | <LegacyTileHostLayer: 0x7a8d32e0> (layer)
   |    |    |    | <UIImageView: 0x7d04bc00; frame = (-39 -5.5; 36 2.5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x7d04ac40>> - (null)
   |    |    |    | <UIImageView: 0x7d04b3a0; frame = (-5.5 -39; 2.5 36); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x7d04ae40>> - (null)
    View not found in container hierarchy: <UITableView: 0x7bac8e00; frame = (0 0; 320 568); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x7ae0a480>; layer = <CALayer: 0x7ae04220>; contentOffset: {0, 0}; contentSize: {320, 264}>
    That view's superview: NO SUPERVIEW
2016-07-09 22:00:36.677 En Yakın[20923:1114504] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view.  Does the constraint reference something from outside the subtree of the view?  That's illegal. constraint:<NSLayoutConstraint:0x7d04cc40 GADBannerView:0x7a8a8c70.bottom == UITableView:0x7bac8e00.bottom> view:<GADBannerView: 0x7a8a8c70; frame = (0 0; 320 50); clipsToBounds = YES; layer = <CALayer: 0x7af35fb0>>'

为了正确执行,应该更改什么?

1 个答案:

答案 0 :(得分:1)

  

我的故事板中有一个UITableViewController。我试图在其底部添加Google广告横幅视图(基本上是UIView)。

不要将自己的子视图添加到表格视图中。而是具有表格视图广告横幅视图。因此,您的视图层次结构将如下所示:

mainview
    tableview
    adbannerview

现在,当然,这意味着mainview不再可以由UITableViewController控制,因为它不是一个tableview。因此,您需要配置包含的视图控制器,以便您具有与正在构建的视图层次结构平行的视图控制器层次结构:

MainViewController
    TableViewController