2
votes

I have a UIContainerView with Frame = {X=20,Y=88,Width=728,Height=660} and a UIScrollView embedded inside it.

The UIScrollView reports Frame = {X=0,Y=0,Width=768,Height=960} that results in an offset of 40 (the section in gold) in the width when paging the UIScrollView. It results in the following output having paged once to the left:

enter image description here

However, ContentSizeForViewInPopover reports the expected Frame size = {Width=728, Height=660}. How can I correct the UIScrollView frame to have the correct size?

Here is the code for ViewDidLoad() of the embedded UIViewController:

var pages = PageControl.Pages = 2;
Console.WriteLine("ScrollView Frame: {0}", ScrollView.Frame);

for(int i = 0; i < pages; i++)
{
    RectangleF frame
    frame.X = ScrollView.Frame.Width * i; // expected {728 * i} but is {768 * i}
    frame.Y = 0;
    frame.Size = ScrollView.Frame.Size;

    Console.WriteLine("Subview frame: {0}", frame);

    ScrollView.AddSubview(
        new UIView(frame) { 
            Bounds = frame,
            BackgroundColor = GetRandomColor() 
        });
    }

    ScrollView.ContentSize = new SizeF(ScrollView.Frame.Width * pages, ScrollView.Frame.Height);
    Console.WriteLine("ScrollView ContentSize = {0}", ScrollView.ContentSize);
4
Where in your code is the UIScrollView created? You have to set its Frame (not ContentSize!) to the superview's bounds and give it proper auto resizing settings.Krumelur
The UIScrollView is set on a UIView which is placed inside a UIContainerView on a StoryBoard. The UIScrollView is not explicitly created.sduplooy

4 Answers

4
votes

call [self.ScrollView layoutIfNeeded]; before this line

frame.X = ScrollView.Frame.Width * i; // expected {728 * i} but is {768 * i}

it works with me, goodluck

2
votes

Double check that in IB under the parent UIViewController, under the Attributes Inspector, Layout section, that the 'Adjust Scroll View Insets' is unchecked. I had a similar issue that tripped me up for awhile and that was all I needed to do to get the proper layout.

0
votes

In IOS 7.0 Use self.automaticallyAdjustsScrollViewInsets=NO; in ViewDidLoad

-3
votes

Try create your UIScrollView in the code, apparently the designer overrides any adjustment you apply in the code.