UIScrollView内的内容大小不正确

时间:2018-06-01 15:26:45

标签: ios xamarin.ios uiscrollview nslayoutconstraint

我在UIScrollView中添加了多个UIViews(扩展类)。所有子视图的大小都通过约束来处理。但是子视图的宽度没有被正确分配。即使UIScrollView填充UIViewController,子视图宽度也不会填充UIScrollView。请找到以下代码。

public partial class MyStats : UIViewController
    {
        private CourseOverview _courseOverviewView;
        private UserStats _userStatsView;
        private LastLogged _lastLogged;
        private AssignmentOverview _assignmentOverView;

        public MyStats() : base("MyStats", null)
        {
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            _courseOverviewView = CourseOverview.CreateView();
            _courseOverviewView.TranslatesAutoresizingMaskIntoConstraints = false;
            _userStatsView = UserStats.CreateView();
            _userStatsView.TranslatesAutoresizingMaskIntoConstraints = false;
            _lastLogged = LastLogged.CreateView();
            _lastLogged.TranslatesAutoresizingMaskIntoConstraints = false;
            _assignmentOverView = AssignmentOverview.CreateView();
            _assignmentOverView.TranslatesAutoresizingMaskIntoConstraints = false;
            ScrollableContainer.AddSubview(_courseOverviewView);
            ScrollableContainer.AddSubview(_userStatsView);
            ScrollableContainer.AddSubview(_lastLogged);
            ScrollableContainer.AddSubview(_assignmentOverView);
        }

        public override void UpdateViewConstraints()
        {
            base.UpdateViewConstraints();
            SetLeadingTrailingConstraints(ScrollableContainer, _courseOverviewView);
            SetHeightConstraint(ScrollableContainer, _courseOverviewView);
            NSLayoutConstraint courseViewTop = NSLayoutConstraint.Create(_courseOverviewView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, ScrollableContainer, NSLayoutAttribute.Top, 1, 10);
            ScrollableContainer.AddConstraint(courseViewTop);

            SetLeadingTrailingConstraints(ScrollableContainer, _userStatsView);
            SetHeightConstraint(ScrollableContainer, _userStatsView, true);
            NSLayoutConstraint statsViewTop = NSLayoutConstraint.Create(_userStatsView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, _courseOverviewView, NSLayoutAttribute.Bottom, 1, 10);
            ScrollableContainer.AddConstraint(statsViewTop);

            SetLeadingTrailingConstraints(ScrollableContainer, _lastLogged);
            NSLayoutConstraint lastLoggedTop = NSLayoutConstraint.Create(_lastLogged, NSLayoutAttribute.Top, NSLayoutRelation.Equal, _userStatsView, NSLayoutAttribute.Bottom, 1, 10);
            ScrollableContainer.AddConstraint(lastLoggedTop);

            SetLeadingTrailingConstraints(ScrollableContainer, _assignmentOverView);
            SetHeightConstraint(ScrollableContainer, _assignmentOverView);
            NSLayoutConstraint overviewTop = NSLayoutConstraint.Create(_assignmentOverView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, _lastLogged, NSLayoutAttribute.Bottom, 1, 10);
            ScrollableContainer.AddConstraint(overviewTop);

            NSLayoutConstraint bottom = NSLayoutConstraint.Create(_assignmentOverView, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, ScrollableContainer, NSLayoutAttribute.Bottom, 1, 10);
            ScrollableContainer.AddConstraint(bottom);

        }

        void SetHeightConstraint(UIView parentView, UIView childView, bool oneFourth = false)
        {
            NSLayoutConstraint height;
            if(oneFourth)
                height = NSLayoutConstraint.Create(childView, NSLayoutAttribute.Height, NSLayoutRelation.Equal, parentView, NSLayoutAttribute.Height, 0.25f, 0);
            else
                height = NSLayoutConstraint.Create(childView, NSLayoutAttribute.Height, NSLayoutRelation.Equal, parentView, NSLayoutAttribute.Height, 0.4f, 0);
            parentView.AddConstraint(height);
        }

        void SetLeadingTrailingConstraints(UIView parentView, UIView childView)
        {
            NSLayoutConstraint left = NSLayoutConstraint.Create(childView, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, parentView, NSLayoutAttribute.Leading, 1, 10);
            NSLayoutConstraint right = NSLayoutConstraint.Create(childView, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, parentView, NSLayoutAttribute.Trailing, 1, -10);
            parentView.AddConstraints(new NSLayoutConstraint[] { left, right });
        }

        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();
        }
    }

0 个答案:

没有答案