Ich habe einen UITableViewController in meinem Storyboard. Ich versuche, eine Google-Werbebanneransicht (die im Grunde eine UIView ist) unten hinzuzufügen.Programmatisch UIView am Ende von UITableView hinzufügen
Ich weiß, ich kann nicht einfach eine UIView in Interface Builder ziehen und es an den unteren Rand der UITableView platzieren. Daher habe ich mich dafür entschieden, die Werbebanneransicht programmatisch hinzuzufügen. Das, was ich codiert:
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])
Wenn ich die app lief und die UITableViewController besuchen, bekomme ich folgende Fehlermeldung:
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>>'
Was geändert werden, um sollte korrekt auszuführen?
So sagen Sie, die UITableViewController werfen und geht mit einem grundlegenden UIViewController durch ein UITableView und das UIView (mein Werbebanner) hinzufügen? –
Hast du gelesen, was ich gesagt habe? Der springende Punkt ist, dass ich Ihnen gezeigt habe, wie _not_ den UITableViewController hinauswirft. – matt
Ich habe es gelesen, aber ein wenig verwirrt. Jetzt habe ich es verstanden. Das tut mir leid. Könnten Sie bitte genauer darauf eingehen? –