2016-05-30 24 views
1

Ich habe eine UICollectionView. Ich habe auch die folgende Ansicht, die ich in Code zu generieren:Programmatisch definierte Einschränkungen Swift iOS

let messageInputContainerView: UIView = { 
    let view = UIView() 
    view.backgroundColor = UIColor.whiteColor() 
    return view 
}() 

Nun, ich mag die messageInputContainerView hinzufügen, so dass es auf den unteren Rand des Bildschirms angebracht ist, und die UICollectionView ist direkt darüber. Im Moment haben ich die folgenden Einschränkungen:

view.addSubview(messageInputContainerView) 
    view.addConstraintsWithFormat("H:|[v0]|", views: messageInputContainerView) 
    view.addConstraintsWithFormat("V:[v0(48)]", views: messageInputContainerView) 

    bottomConstraint = NSLayoutConstraint(item: messageInputContainerView, attribute: .Bottom, relatedBy: .Equal, toItem: view, attribute: .Bottom, multiplier: 1, constant: 0) 
    view.addConstraint(bottomConstraint!) 

Das Problem ist, dass das jetzt Kollektion berührt den unteren Bereich des Bildschirms als auch, und die beiden Ansichten überlappt. Ich habe versucht, dies mit der folgenden Einschränkung zu lösen:

let newConstraint = NSLayoutConstraint(item: messageInputContainerView, attribute: .Top, relatedBy: .Equal, toItem: self.collectionView! , attribute: .Bottom, multiplier: 1, constant: 0) 

view.addConstraint(newConstraint) 

aber dies nur durch ein Bündel von Fehlern:

016-05-30 19:50:26.153 City[1858:79868] Unable to simultaneously satisfy constraints. 
 
\t Probably at least one of the constraints in the following list is one you don't want. 
 
\t Try this: 
 
\t \t (1) look at each constraint and try to figure out which you don't expect; 
 
\t \t (2) find the code that added the unwanted constraint or constraints and fix it. 
 
\t (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
 
(
 
    "<NSAutoresizingMaskLayoutConstraint:0x7fece04d10e0 h=-&- v=-&- UICollectionView:0x7fece08c0a00.midY == UICollectionViewControllerWrapperView:0x7fece04125f0.midY>", 
 
    "<NSAutoresizingMaskLayoutConstraint:0x7fece04d1150 h=-&- v=-&- UICollectionView:0x7fece08c0a00.height == UICollectionViewControllerWrapperView:0x7fece04125f0.height>", 
 
    "<NSLayoutConstraint:0x7fece049a460 V:[UIView:0x7fece04992d0(48)]>", 
 
    "<NSLayoutConstraint:0x7fece04d7620 UIView:0x7fece04992d0.bottom == UICollectionViewControllerWrapperView:0x7fece04125f0.bottom>", 
 
    "<NSLayoutConstraint:0x7fece04a9990 V:[UICollectionView:0x7fece08c0a00]-(0)-[UIView:0x7fece04992d0]>" 
 
) 
 

 
Will attempt to recover by breaking constraint 
 
<NSLayoutConstraint:0x7fece04a9990 V:[UICollectionView:0x7fece08c0a00]-(0)-[UIView:0x7fece04992d0]> 
 

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

+0

Wenn Sie iOS9 und höher unterstützen - würde ich Ihnen WIRKLICH empfehlen, in 'NSLayoutAnchor' Klasse zu schauen, die Ihnen erlaubt, Einschränkungen programmatisch VIEL einfacher zu schreiben. – Soberman

+0

Um Constraints direkt mit NSLayoutConstraint und stringed Format zu schreiben, ist keine gute Idee. Versuchen Sie, https://github.com/robb/Cartography zu verwenden, das freundlichere Schnittstelle hat. – dydus0x14

Antwort

2

Bitte geben Sie die Grund tutorials für automatische Layout der Kasse. Es gibt Ihnen einfach den benötigten Hintergrund für IB und Einschränkungen.

Das Debug-Protokoll im Allgemeinen alles, was Sie brauchen - es sagt Ihnen die genaue Information für Ihr Problem. Lassen Sie uns darüber nachdenken, es im Detail

NSLayoutConstraint:0x7fece04a9990 V:[UICollectionView:0x7fece08c0a00]-(0)-[UIView:0x7fece04992d0] 

UICollectionView:0x7fece08c0a00 - es ist Ihre Sammlung Ansicht, es ist einfach UIView:0x7fece04992d0 - es Ihre messageInputContainerView ist -, dass aus der folgenden Einschränkung klar ist:

"<NSLayoutConstraint:0x7fece049a460 V:[UIView:0x7fece04992d0(48)]>" 

, die durch diese Codezeile erzeugt :

view.addConstraintsWithFormat("V:[v0(48)]", views: messageInputContainerView) 

Also, wenn Sie sehen können - uicollection Blick hat NSAutoresizingMaskLayoutC onstraint - also wurden diese standardmäßig generiert. Sie haben zwei Möglichkeiten - richten Sie Ihre Sammlungsansicht Einschränkungen über IB - setzen Sie führende, trainling, oben (auf welchen Wert Sie benötigen) und unteren Randbedingung mit dem Wert 48.

Die zweite - entfernen automatisch generierte Constraint programmgesteuert und neu hinzufügen eins, wie folgt:

[collectionView removeConstraints: [collectionView constraints]]; 
     // top constraint 
    view.addConstraint(NSLayoutConstraint(item: self.collectionView, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1, constant: 0)) 
    // leading constraint 
    view.addConstraint(NSLayoutConstraint(item: self.collectionView, attribute: .Leading, relatedBy: .Equal, toItem: view, attribute: .Leading, multiplier: 1, constant: 0)) 
    // trailing constaint 
    view.addConstraint(NSLayoutConstraint(item: self.collectionView, attribute: .Trailing, relatedBy: .Equal, toItem: view, attribute: .Trailing, multiplier: 1, constant: 0)) 
    // your constraint 
    let newConstraint = NSLayoutConstraint(item: messageInputContainerView, attribute: .Top, relatedBy: .Equal, toItem: self.collectionView! , attribute: .Bottom, multiplier: 1, constant: 0) 

    view.addConstraint(newConstraint) 

Hoffe, dass dies hilft.