Swift программно устанавливает журнал ошибок ограничения просмотра - PullRequest
1 голос
/ 03 августа 2020

У меня есть UIViewController с парой представлений, и одно из них - UIView, которое содержит представление рекламы (AdMob).

Вот как я устанавливаю представление Constraint:

selectBtn.anchor(top: view.safeAreaLayoutGuide.topAnchor, leading: nil, bottom: nil, trailing: nil, padding: .init(top: 50.0, left: 10.0, bottom: 0, right: 10.0))
selectBtn.centerXToSuperview()
        
statusLabel.anchor(top: selectBtn.bottomAnchor, leading: view.safeAreaLayoutGuide.leadingAnchor, bottom: nil, trailing: view.safeAreaLayoutGuide.trailingAnchor, padding: .init(top: 20.0, left: 10.0, bottom: 0, right: 10.0))

fileSizeLabel.anchor(top: statusLabel.bottomAnchor, leading: view.safeAreaLayoutGuide.leadingAnchor, bottom: nil, trailing: view.safeAreaLayoutGuide.trailingAnchor, padding: .init(top: 10.0, left: 10.0, bottom: 0, right: 10.0))

adView.anchor(top: nil, leading: view.safeAreaLayoutGuide.leadingAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, trailing: view.safeAreaLayoutGuide.trailingAnchor, padding: .init(top: 0.0, left: 0.0, bottom: -50.0, right: 0.0), size: .init(width: view.width, height: 50.0))

imageView.anchor(top: fileSizeLabel.bottomAnchor, leading: view.safeAreaLayoutGuide.leadingAnchor, bottom: adView.topAnchor, trailing: view.safeAreaLayoutGuide.trailingAnchor, padding: .init(top: 10.0, left: 10.0, bottom: 10.0, right: 10.0))

adView находится в полном размере, но находится под обзором из-за дополнения, которое я добавляю (я хочу отображать adview только тогда, когда объявление доступно).

Когда объявление доступно Я вызываю этот код (чтобы отобразить adview):

adView.anchor(top: nil, leading: view.safeAreaLayoutGuide.leadingAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, trailing: view.safeAreaLayoutGuide.trailingAnchor, padding: .init(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0), size: .init(width: view.width, height: 50.0))

Проблема в том, что я получаю эту ошибку:

[LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x600001c34af0 UIView:0x7fbdf040e130.bottom == UILayoutGuide:0x60000061ae60'UIViewSafeAreaLayoutGuide'.bottom + 50   (active)>",
    "<NSLayoutConstraint:0x600001c3c2d0 UIView:0x7fbdf040e130.bottom == UILayoutGuide:0x60000061ae60'UIViewSafeAreaLayoutGuide'.bottom   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600001c34af0 UIView:0x7fbdf040e130.bottom == UILayoutGuide:0x60000061ae60'UIViewSafeAreaLayoutGuide'.bottom + 50   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

Это метод привязки I ' m используя:

open func anchor(top: NSLayoutYAxisAnchor?, leading: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, trailing: NSLayoutXAxisAnchor?, padding: UIEdgeInsets = .zero, size: CGSize = .zero) -> AnchoredConstraints {
        
    translatesAutoresizingMaskIntoConstraints = false
    var anchoredConstraints = AnchoredConstraints()
    
    if let top = top {
        anchoredConstraints.top = topAnchor.constraint(equalTo: top, constant: padding.top)
    }
    
    if let leading = leading {
        anchoredConstraints.leading = leadingAnchor.constraint(equalTo: leading, constant: padding.left)
    }
    
    if let bottom = bottom {
        anchoredConstraints.bottom = bottomAnchor.constraint(equalTo: bottom, constant: -padding.bottom)
    }
    
    if let trailing = trailing {
        anchoredConstraints.trailing = trailingAnchor.constraint(equalTo: trailing, constant: -padding.right)
    }
    
    if size.width != 0 {
        anchoredConstraints.width = widthAnchor.constraint(equalToConstant: size.width)
    }
    
    if size.height != 0 {
        anchoredConstraints.height = heightAnchor.constraint(equalToConstant: size.height)
    }
    
    [anchoredConstraints.top, anchoredConstraints.leading, anchoredConstraints.bottom, anchoredConstraints.trailing, anchoredConstraints.width, anchoredConstraints.height].forEach{ $0?.isActive = true }
    
    return anchoredConstraints
}

Есть идеи, в чем проблема?

Ответы [ 2 ]

2 голосов
/ 03 августа 2020

Когда объявление доступно, вы должны поиграть с созданными здесь нижними ограничениями

adView.anchor(top: nil, leading: view.safeAreaLayoutGuide.leadingAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, trailing: view.safeAreaLayoutGuide.trailingAnchor, padding: .init(top: 0.0, left: 0.0, bottom: -50.0, right: 0.0), size: .init(width: view.width, height: 50.0))

, но при создании здесь другого нижнего ограничения

adView.anchor(top: nil, leading: view.safeAreaLayoutGuide.leadingAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, trailing: view.safeAreaLayoutGuide.trailingAnchor, padding: .init(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0), size: .init(width: view.width, height: 50.0))

оно конфликтует со старым 1, поэтому у вас должен быть экземпляр vaiable

var bottomCon:NSLayoutConstraint!

и сначала указать nil для нижнего якоря

adView.anchor(top: nil, leading: view.safeAreaLayoutGuide.leadingAnchor, bottom:nil, trailing: view.safeAreaLayoutGuide.trailingAnchor, padding: .init(top: 0.0, left: 0.0, bottom: -50.0, right: 0.0), size: .init(width: view.width, height: 50.0))

с добавлением его следующим образом

bottomCon = adView.bottomAnchor.constraint(equalTo:view.safeAreaLayoutGuide.bottomAnchor,constant:50)
bottomCon.isActive = true

после этого когда вам нужно показать рекламу с постоянным значением ie

bottomCon.constant = 0
self.view.layoutIfNeeded()
0 голосов
/ 03 августа 2020

Вы предоставляете конфликтующие ограничения для adView.

adView.anchor(top: nil, leading: view.safeAreaLayoutGuide.leadingAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, trailing: view.safeAreaLayoutGuide.trailingAnchor, padding: .init(top: 0.0, left: 0.0, bottom: -50.0, right: 0.0), size: .init(width: view.width, height: 50.0))

Обеспечение нижнего конструирования вместе с заполнением. Удалите один из них, все будет нормально.

...