问题描述
我必须在 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.