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.

UIStackView Alignment does not work

I have tried everything I can to get the alignment of the UIStackview contents to be .centred or .trailing or anything other than the .leading alignment.

It does not work, and beginning to become frustrating.

I'm now wondering if this is a bug or if i'm doing something silly.

I would appreciate any help or pointers to get this working, thanks.
.
.
.

I have created a sample project adapted from one of the Xamarin samples located here: https://github.com/kooldave98/StackViewIssues

Best Answer

Answers

  • DavidOlubajoDavidOlubajo GBMember

    Update: Here's a more simplified and complete UIViewController that demonstrates this issue:

    The buttons never get centered !!!

    public class ViewController : UIViewController
    {
        UIScrollView scrollView;
        UIStackView stackView;
    
    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
    
        View.BackgroundColor = UIColor.White;
    
        scrollView = new UIScrollView ();
        scrollView.TranslatesAutoresizingMaskIntoConstraints = false;
        View.AddSubview (scrollView);
    
        View.AddConstraints (
            NSLayoutConstraint
            .FromVisualFormat (
                "H:|[scrollView]|",
                NSLayoutFormatOptions.AlignAllCenterX,
                new NSDictionary (),
                NSDictionary.FromObjectAndKey (scrollView, new NSString ("scrollView"))
            )
        );
    
        View.AddConstraints (
            NSLayoutConstraint
            .FromVisualFormat (
                "V:|[scrollView]|",
                NSLayoutFormatOptions.AlignAllCenterX,
                new NSDictionary (),
                NSDictionary.FromObjectAndKey (scrollView, new NSString ("scrollView"))
            )
        );
    
        stackView = new UIStackView ();
        stackView.TranslatesAutoresizingMaskIntoConstraints = false;
        stackView.Axis = UILayoutConstraintAxis.Vertical;
        stackView.Alignment = UIStackViewAlignment.Trailing;
        scrollView.AddSubview (stackView);
    
        scrollView.AddConstraints (
            NSLayoutConstraint
            .FromVisualFormat (
                "H:|[stackView]|",
                NSLayoutFormatOptions.AlignAllCenterX,
                new NSDictionary (),
                NSDictionary.FromObjectAndKey (stackView, new NSString ("stackView"))
            )
        );
    
        scrollView.AddConstraints (
            NSLayoutConstraint
            .FromVisualFormat (
                "V:|[stackView]|",
                NSLayoutFormatOptions.AlignAllCenterX,
                new NSDictionary (),
                NSDictionary.FromObjectAndKey (stackView, new NSString ("stackView"))
            )
        );
    
        for (var i = 0; i < 100; i++) {
            var vw = new UIButton (UIButtonType.System);
            vw.SetTitle ("Button", UIControlState.Normal);
            stackView.AddArrangedSubview (vw);
        }
    }
    }
    
Sign In or Register to comment.