Example #1
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            _stackView = new UIStackView
            {
                TranslatesAutoresizingMaskIntoConstraints = false,
                Axis         = UILayoutConstraintAxis.Vertical,
                Alignment    = UIStackViewAlignment.Fill,
                Distribution = UIStackViewDistribution.EqualSpacing,
                Spacing      = 1
            };

            var scroll = new UIScrollView();

            Add(scroll);

            scroll.AddSubview(_stackView);
            scroll.FullSizeOf(View);
            scroll.EnableAutoLayout();

            _stackView.FullSizeOf(scroll);
            _stackView.WidthAnchor
            .ConstraintEqualTo(scroll.WidthAnchor)
            .Active = true;

            for (var i = 0; i < 25; i++)
            {
                _stackView.AddArrangedSubview(
                    GetButton($"Category {i + 1}", i));

                _stackView.AddArrangedSubview(
                    GetContent($"Child of category {i + 1}", i + 100));
            }
        }
Example #2
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            // Create and add Views

            var stackView = new UIStackView
            {
                Axis = UILayoutConstraintAxis.Vertical,
                // make the inner views to fill the whole available width
                Alignment    = UIStackViewAlignment.Fill,
                Distribution = UIStackViewDistribution.EqualSpacing,
                // margin between views
                Spacing = 4,
                // apply padding between the parent view (the scroll) and the stack
                LayoutMargins = new UIEdgeInsets(4, 4, 4, 4),
                // this will make padding actually work
                LayoutMarginsRelativeArrangement = true
            };

            var scroll = new UIScrollView();

            Add(scroll);

            scroll.AddSubview(stackView);

            // Add some views to the stack with random height and color
            // Notice that we use AddArrangedSubview rather than the former AddSubview
            // for UIStackView to manage it. Otherwise it won´t work
            for (var i = 0; i < 20; i++)
            {
                stackView.AddArrangedSubview(GetRandomView());
            }

            // Layout Views

            // For AutoLayout to work, all views and nested views need to set its
            // "TranslatesAutoresizingMaskIntoConstraints" property to true. It´s easy
            // to forget it so I created an extension method that will set it to the view
            // and its subviews
            scroll.EnableAutoLayout();

            // "FullSizeOf" is a method extension to set leading, trailing,
            // bottom and top constraints
            scroll.FullSizeOf(View);
            stackView.FullSizeOf(scroll);

            // constraint needed when the UIStackView is inside the UIScrollView
            stackView.WidthAnchor.ConstraintEqualTo(scroll.WidthAnchor).Active = true;
        }