UIScrollView 中的自动布局使用 Cirrious.FluentLayouts.Touch [英] Autolayouts in UIScrollView using Cirrious.FluentLayouts.Touch

查看:37
本文介绍了UIScrollView 中的自动布局使用 Cirrious.FluentLayouts.Touch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在 scrollView 中使用自动布局做一个 ViewController,但这里有几个问题:

I've to do a ViewController with autolayouts in scrollView, but here is few problems:

public SomeVC() : UIViewController
    {
        _mainScrollView = new UIScrollView {
            ShowsHorizontalScrollIndicator = false,
            ShowsVerticalScrollIndicator = true,
            BackgroundColor = UIColor.Clear,
            ScrollEnabled = true,
            AutoresizingMask = UIViewAutoresizing.FlexibleHeight,
            TranslatesAutoresizingMaskIntoConstraints = true
        };

        _userDataTableView = new UITableView(CGRect.Empty, UITableViewStyle.Grouped);
        _userDataTableView.LayoutIfNeeded();

        _saveButton = new UIButton();

        _menuTableView = new UITableView(CGRect.Empty, UITableViewStyle.Grouped);
        _menuTableView.LayoutIfNeeded();

        _logoutButton = new UIButton();
    }

    public override void LoadView()
    {
        base.LoadView();
        View = _mainScrollView;
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        Add(_userDataTableView);
        Add(_saveButton);
        Add(_menuTableView);
        Add(_logoutButton);

        _mainScrollView.AddConstraints(
            _userDataTableView.AtTopOf(View),
            _userDataTableView.AtLeftOf(View),
            _userDataTableView.AtRightOf(View),
            _userDataTableView.Height().EqualTo(_userDataTableView.ContentSize.Height),

            _saveButton.Below(_userDataTableView, 20),
            _saveButton.AtLeftOf(_mainScrollView, 10),
            _saveButton.AtRightOf(_mainScrollView, 10),
            _saveButton.Height().EqualTo(44),

            _menuTableView.Below(_saveButton, 20),
            _menuTableView.AtLeftOf(_mainScrollView),
            _menuTableView.AtRightOf(_mainScrollView),
            _menuTableView.Height().EqualTo(_menuTableView.ContentSize.Height),

            _logoutButton.Below(_menuTableView, 20),
            _logoutButton.AtLeftOf(_mainScrollView, 10),
            _logoutButton.AtRightOf(_mainScrollView, 10),
            _logoutButton.Height().EqualTo(44)
        );
        _mainScrollView.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
    }

事实上,它可以工作,但是内容宽度大约是屏幕宽度的一半,并且滚动不起作用.如何让它工作?

In fact, it works, but contents width is about half of screen width, and scrolling not working. How to get it works?

据我了解,问题是 - _mainScrollView.ContentSize,但是在使用自动布局时我应该如何以及在哪里设置它?

As far as I understand, the problem is - _mainScrollView.ContentSize, but how and where should I set it, when using autolayouts?

推荐答案

找到的解决方案:首先:

Solution found: Firstly:

        _userDataTableView.AtLeftOf(View),
        _userDataTableView.AtRightOf(View),

无效,我们应该使用:

        _userDataTableView.AtLeftOf(View),
        _userDataTableView.WithSameWidth(View),

如果我们想要边距,我们只需添加

if we want margin, we just add

        _userDataTableView.WithSameWidth(View).Minus(MARGIN)

我们要做的最后一件事是:

And the last thing we have to do:

        _logoutButton.Height().EqualTo(44), 
        _logoutButton.Bottom().EqualTo().BottomOf(_mainScrollView).Plus(10)

最后一行非常重要.它说要滚动查看正确的内容大小.

last line is very important. It says to scrollView right content size.

这篇关于UIScrollView 中的自动布局使用 Cirrious.FluentLayouts.Touch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆