约束冲突 UICollectionView
Constraints Conflict UICollectionVIew
我收到以下错误输出:
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2016-05-29 20:02:36.092 City[353:33890] 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.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x1461c600 h=--& v=--& UIView:0x14629380.midX == + 80.2612>",
"<NSAutoresizingMaskLayoutConstraint:0x1461c630 h=--& v=--& H:[UIView:0x14629380(64.5225)]>",
"<NSAutoresizingMaskLayoutConstraint:0x1451e6a0 h=--& v=--& UIView:0x1460ac80.midX == + 172.364>",
"<NSAutoresizingMaskLayoutConstraint:0x1451e6d0 h=--& v=--& H:[UIView:0x1460ac80(283.272)]>",
"<NSLayoutConstraint:0x1461dbd0 UITextView:0x14b15000'Fabian Delph has been dro...'.leading == UIView:0x14629380.leading + 8>",
"<NSLayoutConstraint:0x1451e500 UITextView:0x14b15000'Fabian Delph has been dro...'.leading == UIView:0x1460ac80.leading + 8>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x1461dbd0 UITextView:0x14b15000'Fabian Delph has been dro...'.leading == UIView:0x14629380.leading + 8>
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.
我的 UICollectionViewCell
中的所有视图都是在以下方法中以编程方式创建的,但在故事板中定义的 messageTextView
除外。我知道某些自动调整大小约束存在问题,但我不确定如何解决。如果我在整个单元格上使用 translatesAutoresizingMaskIntoConstraints = false
,则 collectionView 的屏幕上不会显示任何内容。我无法理解 UiView
约束指的是什么,以及我可以做些什么来解决这个问题。
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("message_cell" , forIndexPath: indexPath) as! DisplayMessageCollectionViewCell
let textBubbleView: UIView = {
let view = UIView()
view.backgroundColor = UIColor(white: 0.95, alpha: 1)
view.layer.cornerRadius = 15
view.layer.masksToBounds = true
return view
}()
let profileImageView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .ScaleAspectFill
imageView.layer.cornerRadius = 15
imageView.layer.masksToBounds = true
imageView.backgroundColor = UIColor.redColor()
return imageView
}()
profileImageView.translatesAutoresizingMaskIntoConstraints = false
cell.messageTextView.translatesAutoresizingMaskIntoConstraints = false
//textBubbleView.translatesAutoresizingMaskIntoConstraints = false
if let messageText = chat_m[indexPath.row].text {
cell.messageTextView.text = messageText
cell.messageTextView.backgroundColor = UIColor.clearColor()
let sender_image = chat_m[indexPath.row].sender_image
cell.senderID = chat_m[indexPath.row].sender?.id
cell.frameView = view
profileImageView.image = sender_image
let size = CGSizeMake(250, 1000)
let options = NSStringDrawingOptions.UsesFontLeading.union(.UsesLineFragmentOrigin)
let estimatedFrame = NSString(string: messageText).boundingRectWithSize(size, options: options, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(14)], context: nil)
print(estimatedFrame.width)
if let user_id = NSUserDefaults.standardUserDefaults().stringForKey("userId") {
if (user_id == chat_m[indexPath.row].sender?.id) {
profileImageView.hidden = true
cell.messageTextView.frame = CGRectMake(view.frame.width - estimatedFrame.width - 16 - 8, 0, estimatedFrame.width + 16, estimatedFrame.height + 20)
textBubbleView.frame = CGRectMake(view.frame.width - estimatedFrame.width - 16 - 8 - 16, -4, estimatedFrame.width + 16 + 8 + 10, estimatedFrame.height + 20 + 6)
textBubbleView.backgroundColor = UIColor(red: (69/255.0), green: (209/255.0), blue: (153/255.0), alpha: 1.0)
cell.messageTextView.textColor = UIColor.whiteColor()
}
else {
profileImageView.hidden = false
cell.messageTextView.textColor = UIColor.blackColor()
textBubbleView.backgroundColor = UIColor(white: 0.95, alpha:1)
cell.messageTextView.frame = CGRectMake(48 + 3, 0, estimatedFrame.width + 15, estimatedFrame.height + 20 )
textBubbleView.frame = CGRectMake(48, 0, estimatedFrame.width + 20, estimatedFrame.height + 20 )
}
}
}
cell.addSubview(textBubbleView)
cell.addSubview(profileImageView)
cell.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-4-[v0(30)]", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": profileImageView]))
cell.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[v0(30)]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": profileImageView]))
cell.sendSubviewToBack(textBubbleView)
cell.addSubview(profileImageView)
cell.bringSubviewToFront(profileImageView)
cell.addConstraint(NSLayoutConstraint(item: cell.messageTextView, attribute: .Trailing, relatedBy: .Equal, toItem: textBubbleView, attribute: .Trailing, multiplier: 1, constant: 0))
cell.addConstraint(NSLayoutConstraint(item:cell.messageTextView, attribute: .Leading, relatedBy: .Equal, toItem: textBubbleView, attribute: .Leading, multiplier: 1, constant: 8))
return cell
}
来自您的调试日志:
"<NSLayoutConstraint:0x1461dbd0 UITextView:0x14b15000'Fabian Delph has been dro...'.leading == UIView:0x14629380.leading + 8>",
"<NSLayoutConstraint:0x1451e500 UITextView:0x14b15000'Fabian Delph has been dro...'.leading == UIView:0x1460ac80.leading + 8>"
您有两个具有相同值的约束(0x1461dbd0、0x1451e500)绑定到不同的视图。这可能会发生冲突。
您的 textBubbleView
有不同的框架,这取决于您的逻辑,并且没有适当的约束(现在它使用默认的 translatesAutoresizingMaskIntoConstraints,其值为 YES)。您有注释行,这是正确的方法,但您还必须按照对 profileImageView 所做的方式设置 textBubbleView
的约束(前导、尾随、顶部、高度 - 这取决于您的要求。
如果您希望 messageTextView
的布局取决于 textBubbleView
-
先删除旧约束并重新添加它:
[messageTextView removeConstraints: [messageTextView constraints]];
我收到以下错误输出:
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2016-05-29 20:02:36.092 City[353:33890] 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.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x1461c600 h=--& v=--& UIView:0x14629380.midX == + 80.2612>",
"<NSAutoresizingMaskLayoutConstraint:0x1461c630 h=--& v=--& H:[UIView:0x14629380(64.5225)]>",
"<NSAutoresizingMaskLayoutConstraint:0x1451e6a0 h=--& v=--& UIView:0x1460ac80.midX == + 172.364>",
"<NSAutoresizingMaskLayoutConstraint:0x1451e6d0 h=--& v=--& H:[UIView:0x1460ac80(283.272)]>",
"<NSLayoutConstraint:0x1461dbd0 UITextView:0x14b15000'Fabian Delph has been dro...'.leading == UIView:0x14629380.leading + 8>",
"<NSLayoutConstraint:0x1451e500 UITextView:0x14b15000'Fabian Delph has been dro...'.leading == UIView:0x1460ac80.leading + 8>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x1461dbd0 UITextView:0x14b15000'Fabian Delph has been dro...'.leading == UIView:0x14629380.leading + 8>
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.
我的 UICollectionViewCell
中的所有视图都是在以下方法中以编程方式创建的,但在故事板中定义的 messageTextView
除外。我知道某些自动调整大小约束存在问题,但我不确定如何解决。如果我在整个单元格上使用 translatesAutoresizingMaskIntoConstraints = false
,则 collectionView 的屏幕上不会显示任何内容。我无法理解 UiView
约束指的是什么,以及我可以做些什么来解决这个问题。
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("message_cell" , forIndexPath: indexPath) as! DisplayMessageCollectionViewCell
let textBubbleView: UIView = {
let view = UIView()
view.backgroundColor = UIColor(white: 0.95, alpha: 1)
view.layer.cornerRadius = 15
view.layer.masksToBounds = true
return view
}()
let profileImageView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .ScaleAspectFill
imageView.layer.cornerRadius = 15
imageView.layer.masksToBounds = true
imageView.backgroundColor = UIColor.redColor()
return imageView
}()
profileImageView.translatesAutoresizingMaskIntoConstraints = false
cell.messageTextView.translatesAutoresizingMaskIntoConstraints = false
//textBubbleView.translatesAutoresizingMaskIntoConstraints = false
if let messageText = chat_m[indexPath.row].text {
cell.messageTextView.text = messageText
cell.messageTextView.backgroundColor = UIColor.clearColor()
let sender_image = chat_m[indexPath.row].sender_image
cell.senderID = chat_m[indexPath.row].sender?.id
cell.frameView = view
profileImageView.image = sender_image
let size = CGSizeMake(250, 1000)
let options = NSStringDrawingOptions.UsesFontLeading.union(.UsesLineFragmentOrigin)
let estimatedFrame = NSString(string: messageText).boundingRectWithSize(size, options: options, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(14)], context: nil)
print(estimatedFrame.width)
if let user_id = NSUserDefaults.standardUserDefaults().stringForKey("userId") {
if (user_id == chat_m[indexPath.row].sender?.id) {
profileImageView.hidden = true
cell.messageTextView.frame = CGRectMake(view.frame.width - estimatedFrame.width - 16 - 8, 0, estimatedFrame.width + 16, estimatedFrame.height + 20)
textBubbleView.frame = CGRectMake(view.frame.width - estimatedFrame.width - 16 - 8 - 16, -4, estimatedFrame.width + 16 + 8 + 10, estimatedFrame.height + 20 + 6)
textBubbleView.backgroundColor = UIColor(red: (69/255.0), green: (209/255.0), blue: (153/255.0), alpha: 1.0)
cell.messageTextView.textColor = UIColor.whiteColor()
}
else {
profileImageView.hidden = false
cell.messageTextView.textColor = UIColor.blackColor()
textBubbleView.backgroundColor = UIColor(white: 0.95, alpha:1)
cell.messageTextView.frame = CGRectMake(48 + 3, 0, estimatedFrame.width + 15, estimatedFrame.height + 20 )
textBubbleView.frame = CGRectMake(48, 0, estimatedFrame.width + 20, estimatedFrame.height + 20 )
}
}
}
cell.addSubview(textBubbleView)
cell.addSubview(profileImageView)
cell.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-4-[v0(30)]", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": profileImageView]))
cell.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[v0(30)]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": profileImageView]))
cell.sendSubviewToBack(textBubbleView)
cell.addSubview(profileImageView)
cell.bringSubviewToFront(profileImageView)
cell.addConstraint(NSLayoutConstraint(item: cell.messageTextView, attribute: .Trailing, relatedBy: .Equal, toItem: textBubbleView, attribute: .Trailing, multiplier: 1, constant: 0))
cell.addConstraint(NSLayoutConstraint(item:cell.messageTextView, attribute: .Leading, relatedBy: .Equal, toItem: textBubbleView, attribute: .Leading, multiplier: 1, constant: 8))
return cell
}
来自您的调试日志:
"<NSLayoutConstraint:0x1461dbd0 UITextView:0x14b15000'Fabian Delph has been dro...'.leading == UIView:0x14629380.leading + 8>",
"<NSLayoutConstraint:0x1451e500 UITextView:0x14b15000'Fabian Delph has been dro...'.leading == UIView:0x1460ac80.leading + 8>"
您有两个具有相同值的约束(0x1461dbd0、0x1451e500)绑定到不同的视图。这可能会发生冲突。
您的 textBubbleView
有不同的框架,这取决于您的逻辑,并且没有适当的约束(现在它使用默认的 translatesAutoresizingMaskIntoConstraints,其值为 YES)。您有注释行,这是正确的方法,但您还必须按照对 profileImageView 所做的方式设置 textBubbleView
的约束(前导、尾随、顶部、高度 - 这取决于您的要求。
如果您希望 messageTextView
的布局取决于 textBubbleView
-
先删除旧约束并重新添加它:
[messageTextView removeConstraints: [messageTextView constraints]];