【问题标题】:Constraint items must each be a view or layout guide每个约束项都必须是视图或布局指南
【发布时间】:2019-05-14 20:32:07
【问题描述】:

我正在尝试在我的 UIViewController 底部添加一个 Admob 横幅:

func addBannerViewToView() {
    bannerView = GADBannerView(adSize: kGADAdSizeBanner)
    bannerView.adUnitID = "ca-app-pub-HIDDEN/HIDDEN"
    bannerView.rootViewController = self
    bannerView.delegate = self
    bannerView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(bannerView)
    view.addConstraints(
        [NSLayoutConstraint(item: bannerView,
                            attribute: .bottom,
                            relatedBy: .equal,
                            toItem: view.safeAreaLayoutGuide.bottomAnchor,
                            attribute: .top,
                            multiplier: 1,
                            constant: 0),
         NSLayoutConstraint(item: bannerView,
                            attribute: .centerX,
                            relatedBy: .equal,
                            toItem: view,
                            attribute: .centerX,
                            multiplier: 1,
                            constant: 0)
        ])
    bannerView.load(GADRequest())
}

我在 viewDidLoad 中调用此函数,但应用程序崩溃:

由于未捕获的异常而终止应用 'NSInvalidArgumentException',原因:'NSLayoutConstraint for >:约束项必须每个都是 查看或布局指南。'

我用了一个官方的例子https://developers.google.com/admob/ios/banner

【问题讨论】:

    标签: ios admob


    【解决方案1】:

    错误在第一个约束中。 iOS 12 中的正确表述是:

    NSLayoutConstraint(item: bannerView,
                       attribute: .bottom,
                       relatedBy: .equal,
                       toItem: view.safeAreaLayoutGuide,
                       attribute: .bottom,
                       multiplier: 1,
                       constant: 0)
    

    这意味着bannerView的属性bottom必须是equalview.safeAreaLayoutGuidebottom属性。

    【讨论】:

      【解决方案2】:

      替换

      toItem: view.safeAreaLayoutGuide.bottomAnchor,
      

      toItem: view.safeAreaLayoutGuide,
      

      或者

      toItem: view,
      

      您指定一个锚点,但例外情况表明它必须是普通视图或 layoutGuide

      【讨论】:

        【解决方案3】:

        这行得通:

        func addBannerViewToView() {
                bannerView = GADBannerView(adSize: kGADAdSizeBanner)
                bannerView.adUnitID = "ca-app-pub-HIDDEN/HIDDEN"
                bannerView.rootViewController = self
                bannerView.delegate = self
                bannerView.translatesAutoresizingMaskIntoConstraints = false
                view.addSubview(bannerView)
                view.addConstraints(
                    [NSLayoutConstraint(item: bannerView,
                                        attribute: .bottom,
                                        relatedBy: .equal,
                                        toItem: view,
                                        attribute: .bottomMargin,
                                        multiplier: 1,
                                        constant: 0),
                     NSLayoutConstraint(item: bannerView,
                                        attribute: .centerX,
                                        relatedBy: .equal,
                                        toItem: view,
                                        attribute: .centerX,
                                        multiplier: 1,
                                        constant: 0)
                    ])
                bannerView.load(GADRequest())
            }
        

        【讨论】:

          猜你喜欢
          • 2021-04-09
          • 2020-10-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-30
          • 1970-01-01
          相关资源
          最近更新 更多