J'ai mis ma vue par code avec NSLayoutConstraint
pour définir la hauteur de ma vue et j'ai une erreur bizarre comme ceci: Quand j'utilise fraction number
comme 324.867
pour définir la hauteur de ma vue. Il déclenche une erreur Unable to simultaneously satisfy constraints
. Mais si je arrondis 324.867
à 324
. L'erreur est partie.Pourquoi quand j'utilise le numéro de fraction, j'ai l'erreur [LayoutConstraints]. Mais quand je arrondis ce nombre. L'erreur est allée
Quelqu'un peut-il expliquer pourquoi cela arrive?
Ceci est mon code:
let estimateSizeOfTopic = CGSize(width: self.frame.width - 10.0, height: 1000.0)
let attributeOfTopic = [NSFontAttributeName: UIFont.systemFont(ofSize: 14)]
let estimateHeight = NSString(string: topicTitle.text!).boundingRect(with: estimateSizeOfTopic, options: .usesLineFragmentOrigin, attributes: attributeOfTopic, context: nil).height
NSLayoutConstraint(item: topicTitle, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: estimateHeight).isActive = true
NSLayoutConstraint(item: topicTitle, attribute: .top, relatedBy: .equal, toItem: superView, attribute: .top, multiplier: 1.0, constant: 5.0).isActive = true
NSLayoutConstraint(item: topicTitle, attribute: .left, relatedBy: .equal, toItem: superView, attribute: .left, multiplier: 1.0, constant: 5.0).isActive = true
NSLayoutConstraint(item: topicTitle, attribute: .right, relatedBy: .equal, toItem: superView, attribute: .right, multiplier: 1.0, constant: -5.0).isActive = true
NSLayoutConstraint(item: topicTitle, attribute: .bottom, relatedBy: .equal, toItem: superView, attribute: .bottom, multiplier: 1.0, constant: -5.0).isActive = true
// Mon Superview n'a pas de contrainte ... Je codé de cette façon parce que je veux à la hauteur de réglage automatique de mes Superview de par ma taille de TOPICTITLE (tableau Afficher le dimensionnement automatique des cellules)
L'erreur se produit à la première contrainte.
//If I use this line of code. It raise an error
NSLayoutConstraint(item: topicTitle, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: estimateHeight).isActive = true
//If I use this line of code instead. The error is gone
NSLayoutConstraint(item: topicTitle, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: round(estimateHeight)).isActive = true
J'arrondi mon estimateHeight
parce que si je n'arrondit pas, il soulève l'erreur .. Mais Inspire il y a une erreur. Toutes les choses à mon avis encore regarder droit
C'est ce que les fenêtres de débogage dit:
[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:0x608000484560 UITextView:0x7f9049080c00'It is a long established ...'.height == 324.867 (active)>",
"<NSLayoutConstraint:0x608000285280 V:|-(5)-[UITextView:0x7f9049080c00'It is a long established ...'] (active, names: '|':SatacusWorld.PostViewCell:0x7f9049096a00'postIdentifier')>",
"<NSLayoutConstraint:0x60800029fe50 UITextView:0x7f9049080c00'It is a long established ...'.bottom == SatacusWorld.PostViewCell:0x7f9049096a00'postIdentifier'.bottom - 5 (active)>",
"<NSLayoutConstraint:0x60800029fcc0 'UIView-Encapsulated-Layout-Height' SatacusWorld.PostViewCell:0x7f9049096a00'postIdentifier'.height == 334.8 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x608000484560 UITextView:0x7f9049080c00'It is a long established ...'.height == 324.867 (active)>
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.