7
votes

I am getting strange behavior with UIScrollView subviews, the idea is to create programmatically an instance of UIView with a customized nib file which is a form in my case, fill that form with data from a model class, and add it as subview for my UIScrollView. The problem is when I deal with more than one subview, the UIScrollView only keep the latest subview, so if I created three subviews, the scrollview will show only the third (the latest) subview. Although the page control is set to the coreect number of subviews (three).

The project is too long, so I will try to explain brievely my issue with the relevant code:

- (void)viewDidLoad {

    NSArray *sorted = [appArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];//this array contains the data from model, I debugged that to make sure I got the exact data, no more no less :)


    //Loop all NSManaged objects, for example let's say I have 3 model objects, don't worry about how I get data etc, because I debugged all and maked sure all data objects number are exact, etc.
    for (App_Table *_appTable in sorted) {
    //This looped 3 times as expected, I debugged that also and maked sure on each iteration I got the data I expected to have
    //App_Table is a subclass of NSManagedObject, it's my model class and get its data from coredata file
    [self addMoreView];//Call this method will create a new subview and add it as subview to the UIScrollView, it will also update the page control, update the content size property, etc.

    AppTableView *_appTableView = (AppTableView *) [[self.scrollView subviews] lastObject];//Remember addMoreView method create a new instance of AppTableView and add it as subview for the UIScrollView property, then I get that subview to fill it with data here

    _appTableView.txtADDRESS.text = _appTable.address;//Fill in the form, no need to write all the form fields code because it's the same way.

    // Scroll To First Page...
    self.pageControl.currentPage = 0;
    [self.scrollView scrollRectToVisible:CGRectMake(0, 0, viewWidth, viewHeight) animated:YES];

    self.scrollView.contentSize = CGSizeMake(viewWidth*noOfItems, viewHeight);//Set the content size to the sum of subviews width, I also debugged that to check it's correct
    self.scrollView.delegate = self;

    [super viewDidLoad];
}

Ok, so when I have three subviews, the scrollview will load with the width of three subviews as calculated with the line above:

self.scrollView.contentSize = CGSizeMake(viewWidth*noOfItems, viewHeight);//Set the content size to the sum of subviews width, I also debugged that to check it's correct

And I can scroll till 3 moves (which is the number of subviews) and the UIPageControl is also set to three dots, but only ONE subview is visible, only the latest one I can see, the two other subviews disappeared, the scroll view calculated content size for them but they are not visible. Any thoughts ? Thanx.

EDIT:

It's worth to note that the first time I edit the view, all goes fine, when I deal with 3 subviews, they are all visible, but when I go to another view and get back to this view, only the last subview is visible.

Also, I am working on an iPad project for that with a split view.

EDIT: This is the code of the method which draw new subview for the UIScrollView

 -(IBAction) addMoreView
    {

        NSArray *arr = [[NSBundle mainBundle] loadNibNamed:@"AppTableView" owner:self options:nil];

        AppTableView *aView = [arr objectAtIndex:0];
        [aView setFrame:CGRectMake(startX, 0, aView.bounds.size.width, aView.bounds.size.height)];
        [self.scrollView addSubview:aView];
        self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width+aView.frame.size.width
                                             , self.scrollView.contentSize.height);


        startX = startX + aView.frame.size.width;//Update the X position, first 0, then 600, 1200, and so on.
        [self.scrollView scrollRectToVisible:aView.frame animated:YES];
        NSLog(@"%f",self.scrollView.contentSize.width);
        NSLog(@"%f",self.scrollView.contentSize.height);

    }

Suppose I have 3 subviews, the method above will be called 3 times since it's put inside the for loop. So here is the result of NSLogs for the above method:

NSLog(@"%f",self.scrollView.contentSize.width);
NSLog(@"%f",self.scrollView.contentSize.height);

    First iteration:

    600.000000

    0.000000

    Second iteration:

    1200.000000

    0.000000

    Third iteration:

    1800.000000

    0.000000

EDIT:

The command suggested by rob mayoff allows me to see why this happen:

   |    |    |    | <AppTableView: 0x1eef4700; frame = (0 18; 600 430); autoresize = LM+RM+TM+BM; tag = 990; layer = <CALayer: 0x1eef4790>>

   |    |    |    | <AppTableView: 0x1ee3f380; frame = (0 18; 600 430); autoresize = LM+RM+TM+BM; tag = 991; layer = <CALayer: 0x1ee3f410>>

   |    |    |    | <AppTableView: 0x1ee56910; frame = (0 18; 600 430); autoresize = LM+RM+TM+BM; tag = 992; layer = <CALayer: 0x1ee3f410>>

All Three subviews are drawn in the same frame, so they are above each other, but when I debug that with breakpoints in runtime, the x is changing, first time 0, then 600 then 1200 which make me think it's drawing correctly. How to fix that especially that the x value is being incremented correctly, so what's the problem and why they still drawing on the same x coordinate?

7
When you say "go to another view and get back to this view", is this view being rebuilt? I'm guessing that it's not; that -viewDidLoad is only called before the first time, and the issue is somewhere else in your code.Seamus Campbell
Yes, it's being rebuilt, because when I quit this view, data is stored to the model, then when I go back, data is retrieved and subviews are rebuilt. What you mean -viewDidLoad is only called before the first time I checked with breakpoints and viewDidLoad is always called when getting to the view.Malloc
Okay, then if you get a different result in different calls to -viewDidLoad, what is changing between the two calls?Seamus Campbell
Hi, In each call, I checked the contentSize, frame, data, etc. All is fine, Why the scrollview is always keeping the last iteration in the for loop, although it draws the necessar space to hold all subviews, but only showing up one of them?Malloc
Where does your for loop end? And you probably need to show us the code for -addMoreView.Seamus Campbell

7 Answers

2
votes

First things first, you should place the [super viewDidLoad] call at the top of your - (void)viewDidLoad method, not at the bottom.

Given that it's not clear enough what you are actually seeing and what you expect to see, providing a minimum working example of your problem as a downloadable project would help us to help you. If you just try to reproduce this in a separate view controller that does not interact with managed objects, but uses some statically provided data, others will be able to reproduce it themselves and debug it. Or you might just as well figure it out yourself in the process.

1
votes

I am getting back a bit late, but finally I figure out the fix for my problem so thought it's good to share it to save someone else's time.

For my case, the scrollview is a custom view in a nib file. So by activating its Autosizing masks left and top, the bug was fixed.

enter image description here

0
votes

This block of code in -addMoreView is the problem:

AppTableView *aView = [arr objectAtIndex:0];
[aView setFrame:CGRectMake(startX, 0, aView.bounds.size.width, aView.bounds.size.height)];
[self.scrollView addSubview:aView];

If startX never changes, you're adding all of these views at the same spot.

0
votes

Surely your not setting the content size correctly for 3 items:

self.scrollView.contentSize.width+aView.frame.size.width

should be

self.scrollView.contentSize.width+ ( aView.frame.size.width * NUMBER_OF_SUBVIEWS)

I presume when you scroll horizontally on the scrollview, the content size only allows enough room for one subview correct ?

0
votes

used a static variable which is reinitialized to 0 on load, then it gets incremented by the width of the subview to start drawing on new X position.

0
votes

Try to set the autoresizingMask of aView to UIViewAutoresizingNone inside the addMoreView method.

0
votes

I created a project that uses your code in the sister post, and its working just fine. I added several tests on the autoresizing masks as you can see below. I suggest you compare it to what you are doing. Weird things happen when the masks on EITHER the subviews or the scrollView are not set to being tied to the top/left.

My modified code:

NSArray *colors = @[ [UIColor redColor], [UIColor greenColor], [UIColor blueColor] ];

UIViewAutoresizing mask;
mask = [self.scrollView autoresizingMask];
//assert(!self.scrollView.autoresizesSubviews); // has no affect here

if((mask & (UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin)) != mask) NSLog(@"scrollView MASK WRONG");

for (int i=0; i<3; ++i) {

    NSArray *arr = [[NSBundle mainBundle] loadNibNamed:@"AppTableView" owner:self options:nil];

    NSLog(@"scrollView frame: %@", NSStringFromCGRect(self.scrollView.frame));
    if((mask & (UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin)) != mask) NSLog(@"aView MASK WRONG");

    AppTableView *aView = [arr objectAtIndex:0];
    assert([aView isKindOfClass:[AppTableView class]]);
    NSLog(@"orig aView frame: %@", NSStringFromCGRect(aView.frame));
    UIViewAutoresizing mask = [aView autoresizingMask];
    if((mask & (UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin)) != mask) NSLog(@"aView MASK WRONG");


    aView.frame = CGRectMake(startX, 0, aView.bounds.size.width, aView.bounds.size.height);
    NSLog(@"changed aView frame: %@", NSStringFromCGRect(aView.frame));
    aView.backgroundColor = colors[i];

    [self.scrollView addSubview:aView];
    self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width+aView.frame.size.width
                                             ,self.scrollView.contentSize.height);
    startX = startX + aView.frame.size.width;

   //AppTableView *_appTableView = (AppTableView *) [[self.scrollView subviews] lastObject];

    //_appTableView.txtADDRESS.text = _appTable.address;//Fill in the form, no need to write all the form fields code because it's the same way.
}
NSLog(@"scrollView frame: %@", NSStringFromCGRect(self.scrollView.frame));
NSLog(@"scrollView contentOffset: %@", NSStringFromCGPoint(self.scrollView.contentOffset));