在使用自动布局之前,您应该禁用TranslatesAutoresizingMaskIntoConstraints
. 因此,将这两个控件属性设置为 False 会产生影响,此外,您应该更正您的用法:
containerView.TranslatesAutoresizingMaskIntoConstraints = false;
SFCalendar calendar = new SFCalendar();
containerView.AddSubview(calendar);
calendar.TranslatesAutoresizingMaskIntoConstraints = false;
View.AddConstraints(containerView.AtBottomOf(this.View),
containerView.AtTopOf(this.View),
containerView.WithSameWidth(this.View));
containerView.AddConstraints(calendar.AtBottomOf(containerView),
calendar.AtTopOf(containerView),
calendar.WithSameWidth(containerView));
但是我建议您使用本机约束来执行此操作,而且您似乎也想让这个containerView
全屏显示。添加其前导、尾随、顶部、底部会更好:
SFCalendar calendar = new SFCalendar();
containerView.AddSubview(calendar);
containerView.TranslatesAutoresizingMaskIntoConstraints = false;
containerView.LeadingAnchor.ConstraintEqualTo(View.LeadingAnchor).Active = true;
containerView.TopAnchor.ConstraintEqualTo(TopLayoutGuide.GetBottomAnchor()).Active = true;
containerView.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor).Active = true;
containerView.BottomAnchor.ConstraintEqualTo(BottomLayoutGuide.GetTopAnchor()).Active = true;
calendar.TranslatesAutoresizingMaskIntoConstraints = false;
calendar.LeadingAnchor.ConstraintEqualTo(containerView.LeadingAnchor).Active = true;
calendar.TopAnchor.ConstraintEqualTo(containerView.TopAnchor).Active = true;
calendar.TrailingAnchor.ConstraintEqualTo(containerView.TrailingAnchor).Active = true;
calendar.BottomAnchor.ConstraintEqualTo(containerView.BottomAnchor).Active = true;