我一直在我的iOS应用程序中使用Stuart Lodge的FluentLayout,它就像一个魅力,但我遇到了问题,当我尝试在MvxTableViewCell中使用约束时出现错误:
Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2014-08-14 20:20:36.959 VMSiOS[773:70b] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7bbbf710 UIButton:0x7bab3b70.left == UITableViewCellContentView:0x7bbbcc60.left>
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
2014-08-14 20:20:36.960 VMSiOS[773:70b] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7bbbf6c0 UIButton:0x7bab3b70.right == UITableViewCellContentView:0x7bbbcc60.right>
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
2014-08-14 20:20:36.960 VMSiOS[773:70b] The view hierarchy is not prepared for the constraint: <NS
LayoutConstraint:0x7bbbf7e0 UIButton:0x7bab3b70.top == UITableViewCellContentView:0x7bbbcc60.top>
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
2014-08-14 20:20:36.969 VMSiOS[773:70b] View hierarchy unprepared for cons
traint.
Constraint: <NSLayoutConstraint:0x7bbbf710 UIButton:0x7bab3b70.left == UITableViewCellContentView:0x7bbbcc60.left>
Container hierarchy:
<UITableViewCellContentView: 0x7bbbcc60; frame = (0 0; 320 44); gestureRecognizers = <NSArray: 0x7bac2b20>; layer = <CALayer: 0x7bbbcdd0>>
View not found in container hierarchy: <UIButton: 0x7bab3b70; frame = (0 0; 320 104); opaque = NO; layer = <CALayer: 0x7bab7350>>
That view's superview: <UITableViewCellScrollView: 0x7bbbc880; frame = (0 0; 320 104); autoresize = W+H; gestureRecognizers = <NSArray: 0x7bbbc3d0>; layer = <CALayer: 0x7bbbc750>; contentOffset: {0, 0}>
An unhandled exception occured.
我试图约束的UIButton代码看起来像这样
partial class CreateAddTableCellView : MvxTableViewCell
{
public static readonly NSString Key = new NSString("CreateAddCell");
private UIButton _btnAdd;
public CreateAddTableCellView(IntPtr handle)
: base(handle)
{
BackgroundColor = UIColor.FromRGB(141, 198, 63);
_btnAdd = UIButton.FromType(UIButtonType.Custom);
_btnAdd.Frame = new RectangleF(0, 0, Frame.Width, 104);
_btnAdd.BackgroundColor = UIColor.FromRGB(141, 198, 63);
_btnAdd.SetImage(UIImage.FromBundle(@"Icons/create_plus.png"), UIControlState.Normal);
_btnAdd.ImageEdgeInsets = new UIEdgeInsets(-30, (Frame.Width / 2) - (_btnAdd.CurrentImage.Size.Width / 2), 0, 0);
_btnAdd.SetTitle("Create a new style", UIControlState.Normal);
_btnAdd.SetTitleColor(UIColor.White, UIControlState.Normal);
_btnAdd.TitleEdgeInsets = new UIEdgeInsets(60, -65, 0, 0);
ContentView.AddConstraints(new[] {
NSLayoutConstraint.Create(_btnAdd, NSLayoutAttribute.Left, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Left, 1, 0),
NSLayoutConstraint.Create(_btnAdd, NSLayoutAttribute.Right, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Right, 1, 0),
NSLayoutConstraint.Create(_btnAdd, NSLayoutAttribute.Top, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Top, 1, 0),
});
Add(_btnAdd);
this.DelayBind(() =>
{
var set = this.CreateBindingSet<CreateAddTableCellView, CreateTableCellViewModel>();
set.Bind(_btnAdd).To(vm => vm.CreateNewCommand);
set.Apply();
});
}
因为这是一个MvxTableViewCell我找不到任何OnViewDidLoad方法来尝试将约束放在那里而且也没有View对象存在,通过在网上搜索,使用目标C的人表示将约束放在ContentView没有SubviewsDoNotTranslateAutoresizingMaskIntoConstraints()方法。
关于如何使这项工作的任何想法?