1
votes

I have a View which contains a ScrollView. This Scrollview contains a UIView (called ContentView), which in turn has two subviews I'd like to scroll between.

Runtime, these are the values for the various views:

ContentView (inside the UIScrollView):

  Width: 640
  Height: 568

subView (UIView) inside the ContentView:

1

X: 0
Y: 0
Width: 320
Height: 568

2

X: 320
Y: 0
Width: 320
Height: 568 

ScrollView:

Width: 320
Height: 568
ContentSize: 
Width: 640
Height: 568

I add the subviews to the content view programatically (using a for loop) and then change the Bounds of the ContentView and ContentSize of the ScrollView accordingly, like so:

for (var i = 0; i < 2; i++) {
    var page = ViewModel.Items[i];
    var pageView = CreatePage(page);
    pageView.Frame = new CGRect(View.Bounds.Width*i, 0, View.Bounds.Width, View.Bounds.Height);
    ContentView.Add (pageView);
}

ContentView.Frame = new CGRect (0, 0, View.Bounds.Width * 2, View.Bounds.Height);
ScrollView.ContentSize = new CGSize (ContentView.Bounds.Width, ContentView.Bounds.Height);

When I load the application, this is what I get:

enter image description here

Just one of the subviews with no scrolling enabled. How can I get the horizontal scrolling working properly?

Thanks for any replies! :-)

1

1 Answers

0
votes

You have set the ContentView.Frame and ScrollView.ContentSize the wrong way around. The frame of the scrollview should be the same as the bounds of the screen while the contentSize equivalent of the size of the actual content.

The UIScrollview will not scroll unless the content size is larger than the frame of the actual UIScrollView instance.

ContentView.Frame = new CGRect (0, 0, View.Bounds.Width, View.Bounds.Height);
ScrollView.ContentSize = new CGSize (ContentView.Bounds.Width*2, ContentView.Bounds.Height);