2015-05-21 26 views
0

我已經做了的ViewController與滾動型自動版式,但這裏有幾個問題:自動版式在UIScrollView中使用Cirrious.FluentLayouts.Touch

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(); 
    } 

事實上,它的工作原理,但內容寬度的一半左右屏幕寬度和滾動不起作用。如何獲得它的作品?

據我瞭解,問題是 - _mainScrollView.ContentSize,但如何以及在何處,我應該將它設置,使用自動版式時?發現

回答

0

解決方案: 首先:

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

是無效的,而不是這一點,我們應該使用:

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

如果我們想的餘量,只需添加

 _userDataTableView.WithSameWidth(View).Minus(MARGIN) 

我們必須做的最後一件事:

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

最後一行是非常重要的。它說scrollView正確的內容大小。

+0

所以,如果一個滾動型有10點的看法,我怎麼得到它滾動?我只是將10個子視圖放置在頂部的另一個下面,而沒有對底部設置約束? –

1

如果你的觀點不超過屏幕,您將無法做滾動。如果你只有有這樣的事情:

// Create containers 
      contentView = new UIView(); 
      scrollView = new UIScrollView { contentView }; 
      Add(scrollView); 


      contentView.AddSubviews(logo, user, password, loginButton); 


      // Auto layout 
      View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints(); 
      View.AddConstraints(scrollView.FullWidthOf(View)); 
      View.AddConstraints(scrollView.FullHeightOf(View)); 
      View.AddConstraints(
       contentView.WithSameWidth(View), 
       contentView.WithSameHeight(View).SetPriority(UILayoutPriority.DefaultLow) 
      ); 

      scrollView.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints(); 
      scrollView.AddConstraints(contentView.FullWidthOf(scrollView)); 
      scrollView.AddConstraints(contentView.FullHeightOf(scrollView)); 

      // very important to make scrolling work 
      var bottomViewConstraint = contentView.Subviews.Last() 
       .AtBottomOf(contentView).Minus(20); 

      contentView.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints(); 

      contentView.AddConstraints(
        logo.AtTopOf(contentView), 
        logo.WithRelativeWidth(contentView, 0.8f), 

        logo.WithSameCenterX(contentView), 
        logo.WithRelativeHeight(contentView, 0.3f), 

        user.Below(logo, 50), 
        user.WithRelativeWidth(logo, 0.8f), 
        user.WithSameCenterX(logo), 

        password.Below(user), 
        password.WithSameWidth(user), 
        password.WithSameCenterX(user), 

        loginButton.Below(password, 50), 
        loginButton.WithRelativeWidth(password, 0.9f), 
        loginButton.Height().EqualTo(50), 
        loginButton.WithSameCenterX(password) 
       ); 

      contentView.AddConstraints(bottomViewConstraint); 

     } 

我使用這個包,它完美的作品一樣,如果我在xamarin.forms使用stackLayout一個滾動視圖裏面我認爲是最完美的行爲。

Xamarin.IQKeyboardManager from Nuget 

另外,如果你想中心滾動視圖裏面的內容視圖,您將需要補充一點:

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

      var scrollViewBounds = scrollView.Bounds; 
      var containerViewBounds = contentView.Bounds; 

      var scrollViewInsets = UIEdgeInsets.Zero; 
      scrollViewInsets.Top = scrollViewBounds.Size.Height/2.0f; 
      scrollViewInsets.Top -= contentView.Bounds.Size.Height/2.0f; 

      scrollViewInsets.Bottom = scrollViewBounds.Size.Height/2.0f; 
      scrollViewInsets.Bottom -= contentView.Bounds.Size.Height/2.0f; 
      scrollViewInsets.Bottom += 1; 

      scrollView.ContentInset = scrollViewInsets; 
     } 

並且那一切,不要緊,你的內容查看是怎麼回事。您將在滾動視圖和經理中擁有一個居中的概覽視圖,以捕獲鍵盤事件並使您的視圖適應此事件。