【发布时间】:2019-11-14 14:21:21
【问题描述】:
我正在尝试在此处复制此问题的答案:Xamarin iOS: Auto Layout within scroll view?,但不使用界面生成器。代码很简单,我有_scrollView,它的背景颜色为绿色。然后我有_contentView,它的背景颜色为红色。我将_contentView 添加到_scrollView,并将_scrollView 添加到ViewController 中的查看。当我运行它时,我得到的只是绿色控制器,红色的控制器永远不会出现在其中。
public async override void ViewDidLoad()
{
base.ViewDidLoad();
//ui
View.BackgroundColor = UIColor.White;
//views
_scrollView = new UIScrollView();
_scrollView.BackgroundColor = UIColor.Green;
_scrollView.TranslatesAutoresizingMaskIntoConstraints = false;
_contentView = new UIView();
_contentView.TranslatesAutoresizingMaskIntoConstraints = false;
_contentView.BackgroundColor = UIColor.Red;
var label = new UILabel();
label.TranslatesAutoresizingMaskIntoConstraints = false;
label.Text = "Text";
_contentView.AddSubview(label);
_scrollView.AddSubview(_contentView);
View.AddSubviews(new UIView[] { _scrollView });
_scrollView.TopAnchor.ConstraintEqualTo(View.TopAnchor, 100).Active = true;
_scrollView.LeftAnchor.ConstraintEqualTo(View.LeftAnchor, 20).Active = true;
_scrollView.RightAnchor.ConstraintEqualTo(View.RightAnchor, -20).Active = true;
_scrollView.BottomAnchor.ConstraintEqualTo(View.BottomAnchor, -100).Active = true;
_contentView.TopAnchor.ConstraintEqualTo(_contentView.Superview.TopAnchor).Active = true;
_contentView.RightAnchor.ConstraintEqualTo(_contentView.Superview.RightAnchor).Active = true;
_contentView.LeftAnchor.ConstraintEqualTo(_contentView.Superview.LeftAnchor).Active = true;
_contentView.BottomAnchor.ConstraintEqualTo(_contentView.Superview.BottomAnchor).Active = true;
_contentView.WidthAnchor.ConstraintEqualTo(_contentView.Superview.WidthAnchor).Active = true;
label.TopAnchor.ConstraintEqualTo(label.Superview.TopAnchor, 100).Active = true;
label.BottomAnchor.ConstraintEqualTo(label.Superview.BottomAnchor, 100).Active = true;
label.LeftAnchor.ConstraintEqualTo(label.Superview.LeftAnchor, 100).Active = true;
label.RightAnchor.ConstraintEqualTo(label.Superview.RightAnchor, 100).Active = true;
}
【问题讨论】:
标签: c# xamarin xamarin.ios