Search code examples
iphoneiosuiscrollviewsubviewuiscrollviewdelegate

UIScrollview subviews show incorrect frames when enumerated


Hi guys i have an issue i cannot get my head around.

I have broken down this issue into its own project and i can still reproduce it.

I have a scrollview say with a 30 uibutton layed out. (for example)

If i was to enumerate over the the subview and log the frames, the odd one will have a frame that wasnt expected

This occurs even once the view has been drawn on the screen

e.g

ViewDidLoad

 [self.scrollVIew.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

        UIButton* view = (UIButton*)obj;

        NSLog(@"%@",NSStringFromCGRect(view.frame));



    }];

LOG:

2012-07-04 15:46:02.939 BUG SCROLLER[2182:707] {{29, 647}, {72, 37}}

2012-07-04 15:46:02.946 BUG SCROLLER[2182:707] {{119, 647}, {72, 37}}

2012-07-04 15:46:02.950 BUG SCROLLER[2182:707] {{219, 647}, {72, 37}}

2012-07-04 15:46:02.954 BUG SCROLLER[2182:707] {{313, 453}, {7, 7}}

As you can see the bottom log shows completly incorrect frame origins and size.

Oddly what ever the size of the view i always seem to get a size of 7.

This appear reproducable but is not visible on the screen

What is going on here and how can i avoid it from happening?

Thanks Dan


Solution

  • Try this instead :

    [self.scrollVIew.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    if([obj isKindOfClass:[UIButton class]])
    {
            UIButton *currentButton = (UIButton *)obj;
            NSLog(@"%@",NSStringFromCGRect(currentButton.frame));
    }
    }];