无法同时满足约束条件.在SWTableViewCell for Xamarin.iOS
本文关键字:SWTableViewCell for Xamarin iOS 约束条件 满足 | 更新日期: 2023-09-27 18:10:05
我正在移植SWTableCell (GitHub)到Xamarin.iOS。我已经用c#实现了所有的代码,但是我仍然有一个约束的问题。
我收到这个消息:
2014-06-23 11:58:59.004 Mark5MobileiOS[1423:70b] 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)
(
"<NSLayoutConstraint:0xe5cd1b0 UIView:0xe5cc310.right == DocumentListViewCell:0xe5c2280.right>",
"<NSLayoutConstraint:0xe5cc370 H:[DocumentListViewCell:0xe5c2280]-(820)-[UIView:0xe5cc310] (LTR)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0xe5cc370 H:[DocumentListViewCell:0xe5c2280]-(820)-[UIView:0xe5cc310](LTR)>
我想我已经找到了导致这个错误的约束条件,但是我必须放一段更大的代码,这样它才有意义。
protected void Initialize ()
{
_cellScrollView = new SwipeableScrollView ();
_cellScrollView.TranslatesAutoresizingMaskIntoConstraints = false;
_cellScrollView.Delegate = new SwipeableScrollViewDelegate (this);
_cellScrollView.ShowsHorizontalScrollIndicator = false;
_cellScrollView.ScrollsToTop = false;
_cellScrollView.ScrollEnabled = true;
AddSubview (_cellScrollView);
AddConstraints (new[] {
NSLayoutConstraint.Create (_cellScrollView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1.0f, 0.0f),
NSLayoutConstraint.Create (_cellScrollView, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, this, NSLayoutAttribute.Bottom, 1.0f, 0.0f),
NSLayoutConstraint.Create (_cellScrollView, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1.0f, 0.0f),
NSLayoutConstraint.Create (_cellScrollView, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1.0f, 0.0f)
});
_cellContentView = new UIView ();
_cellContentView.AddSubview (ContentView);
_cellScrollView.AddSubview (_cellContentView);
TapGestureRecognizer = new UITapGestureRecognizer (HandleScrollViewTapped);
TapGestureRecognizer.CancelsTouchesInView = false;
_longPressGestureRecognizer = new UILongPressGestureRecognizer (HandleScrollViewLongPressed);
_longPressGestureRecognizer.CancelsTouchesInView = false;
_longPressGestureRecognizer.MinimumPressDuration = 0.2f;
TapGestureRecognizer.Delegate = new SwipeableCellGestureRecognizerDelegate (this, _longPressGestureRecognizer);
_longPressGestureRecognizer.Delegate = new SwipeableCellGestureRecognizerDelegate (this, _longPressGestureRecognizer);
_cellScrollView.AddGestureRecognizer (TapGestureRecognizer);
_cellScrollView.AddGestureRecognizer (_longPressGestureRecognizer);
// Create the left and right utility button views, as well as vanilla UIViews in which to embed them. We can manipulate the latter in order to effect clipping according to scroll position.
// Such an approach is necessary in order for the utility views to sit on top to get taps, as well as allow the backgroundColor (and private UITableViewCellBackgroundView) to work properly.
_leftUtillityClipView = new UIView ();
_leftUtillityClipViewConstraint = NSLayoutConstraint.Create (_leftUtillityClipView, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1.0f, 0.0f);
_leftUtilityButtonsView = new SwipeableUtilityButtonView (_leftUtilityButtons, this, HandleUtilityButtonPressed);
_rightUtilityClipView = new UIView ();
_rightUtilityClipViewConstraint = NSLayoutConstraint.Create (_rightUtilityClipView, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1.0f, 0.0f);
_rightUtilityButtonsView = new SwipeableUtilityButtonView (_rightUtilityButtons, this, HandleUtilityButtonPressed);
// Perform common configuration on both sets of utility items (left and right).
var clipViews = new [] { _leftUtillityClipView, _rightUtilityClipView };
var clipConstraints = new [] { _leftUtillityClipViewConstraint, _rightUtilityClipViewConstraint };
var buttonViews = new [] { _leftUtilityButtonsView, _rightUtilityButtonsView };
var alignmentAttributes = new [] { NSLayoutAttribute.Left, NSLayoutAttribute.Right };
for (int i = 0; i < 2; ++i) {
var clipView = clipViews [i];
var clipConstraint = clipConstraints [i];
var buttonView = buttonViews [i];
var alignmentAttribute = alignmentAttributes [i];
clipView.TranslatesAutoresizingMaskIntoConstraints = false;
clipView.ClipsToBounds = true;
AddSubview (clipView);
AddConstraints (new [] {
// Pin the clipping view to the appropriate outer edges of the cell.
NSLayoutConstraint.Create (clipView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1.0f, 0.0f),
NSLayoutConstraint.Create (clipView, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, this, NSLayoutAttribute.Bottom, 1.0f, 0.0f),
/* THIS LINE */ NSLayoutConstraint.Create (clipView, alignmentAttribute, NSLayoutRelation.Equal, this, alignmentAttribute, 1.0f, 0.0f),
/* OR THIS LINE */ clipConstraint,
});
clipView.AddSubview (buttonView);
AddConstraints (new [] {
// Pin the button view to the appropriate outer edges of its clipping view.
NSLayoutConstraint.Create (buttonView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, clipView, NSLayoutAttribute.Top, 1.0f, 0.0f),
NSLayoutConstraint.Create (buttonView, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, clipView, NSLayoutAttribute.Bottom, 1.0f, 0.0f),
NSLayoutConstraint.Create (buttonView, alignmentAttribute, NSLayoutRelation.Equal, clipView, alignmentAttribute, 1.0f, 0.0f),
// Constrain the maximum button width so that at least a button's worth of contentView is left visible. (The button view will shrink accordingly.)
NSLayoutConstraint.Create (buttonView, NSLayoutAttribute.Width, NSLayoutRelation.LessThanOrEqual, ContentView, NSLayoutAttribute.Width, 1.0f, -SwipeableUtilityButtonView.UtilityButtonWidthDefault)
});
}
}
当我删除其中一个标记行时,消息消失,但代码不能正常工作。你可以在这里找到原始实现SWTableViewCell。M -第146和147行。
我试着翻译代码以及我可以不改变实现,这样我就不会得到这样的错误,但正如你所看到的,我失败了。
我真的很惊讶,因为对我来说,我的实现和原来的实现看起来完全一样。
如有任何帮助,不胜感激。
一旦我完成并测试了我的版本的SWTableViewCell,我会把它放在GitHub上,这样别人就不用浪费时间了;)
我在另一段代码中发现了错误,所以上面的代码片段是正确的