简体   繁体   English

UIScrollView在UIScrollView内垂直居中并滚动

[英]Center Vertically UIStackView inside UIScrollView with scroll

Hello it's been imposible to center a UIStackView inside a ScrollView having scroll working ... 您好,将UIStackView居中在具有滚动功能的ScrollView中是不可能的...

I've tried lot of things but the best one has been this: 我尝试了很多事情,但是最好的事情是这样的:

   stackView = new UIStackView
            {
                Axis = UILayoutConstraintAxis.Vertical,
                Alignment = UIStackViewAlignment.Center,
            };

            scroll = new UIScrollView() { };

            Add(scroll);

            scroll.AddSubview(stackView);

            scroll.EnableAutoLayout();

            scroll.FullSizeOf(View);



            stackView.AddArrangedSubview(logo);
            stackView.AddArrangedSubview(_activityIndicatorView);

//... more items

logo.HeightAnchor.ConstraintEqualTo(72).Active = true;

            enterpriseCodeField.HeightAnchor.ConstraintEqualTo(50).Active = true;
            enterpriseCodeField.WidthAnchor.ConstraintEqualTo(0.65f * View.Frame.Size.Width).Active = true;

//... more items 

 stackView.SetCustomSpacing(35, logo);
            stackView.SetCustomSpacing(35, _activityIndicatorView);

//... more items

            View.AddConstraint(NSLayoutConstraint.Create(stackView, NSLayoutAttribute.CenterY, 0, scroll, NSLayoutAttribute.CenterY, 1, 0));
            View.AddConstraint(NSLayoutConstraint.Create(stackView, NSLayoutAttribute.CenterX, 0, scroll, NSLayoutAttribute.CenterX, 1, 0));

This method scroll.EnableAutoLayout(); 此方法scroll.EnableAutoLayout(); is to: TranslatesAutoresizingMaskIntoConstraints = false; 将:TranslatesAutoresizingMaskIntoConstraints = false;

This method scroll.FullSizeOf(View); 此方法scroll.FullSizeOf(View); is to : set leading trailing top etc... 要:设置领先的尾随等

Anyone can help me please ? 有人可以帮我吗? what I'm doing wrong ? 我做错了什么? In android it's very easy to achieve this behavior ... 在android中,实现此行为非常容易...

Thank you so much. 非常感谢。

You can use the scrollview centerX anchor constraint. 您可以使用scrollview centerX锚点约束。 This way you set the centerX anchor constraint of the scroll view to be the same as the scrollView. 这样,您可以将滚动视图的centerX锚点约束设置为与scrollView相同。

[Updated link] I uploaded a project with some code achieving what you are looking for. [更新的链接]我上传了一个项目,其中包含一些代码,可以实现您所需要的。 Please check it out here -> ScrollViewTest 请在这里检查-> ScrollViewTest

The link should work now 该链接现在应该可以工作了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM