Forum Xamarin.iOS
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Using AutoLayout and UIScrollView programatically

Hello,

I am trying to make a copy of the answer from this question on StackOverflow: questions/46488824/xamarin-ios-auto-layout-within-scroll-view/46539635#46539635 but without using the interface builder. The code is simple, I have _scrollView which has background color green. Then I have _contentView which has background color red. I add _contentView to _scrollView and I add _scrollView to View in the ViewController. When I run this, all I get is Green color, the red one never appears inside of it. Do you have any ideas?

Thank you.

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;
        }

Best Answer

  • jaremahjaremah Member
    Accepted Answer

    For anyone trying to figure it out in the future: github @ /bobekhj/Xamarin.iOS.ScrollViewAutoLayout

    I can't post links yet :)

Answers

  • jaremahjaremah Member
    Accepted Answer

    For anyone trying to figure it out in the future: github @ /bobekhj/Xamarin.iOS.ScrollViewAutoLayout

    I can't post links yet :)

Sign In or Register to comment.