locked
Using AutoLayout and UIScrollView programatically RRS feed

  • Question

  • User365293 posted

    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;
            }
    
    Friday, February 9, 2018 8:12 PM

Answers

  • User365293 posted

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

    I can't post links yet :)

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, February 12, 2018 5:04 PM