0

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

Dan
  • 1,447
  • 2
  • 14
  • 30

2 Answers2

3

UIScrollView has two UIImageView subviews - one for each of the horizontal and vertical scroll indicators. You will need to exclude these views to get the frame values you're looking for.

UIKIT_CLASS_AVAILABLE(2_0) @interface UIScrollView : UIView <NSCoding> {
    ...
    UIImageView* _verticalScrollIndicator;
    UIImageView* _horizontalScrollIndicator;

One approach to avoid the problem is to simply check the type of each subview before doing any work:

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

Since each of the UIImageViews used for the scrollbars has a tag of 0, a second approach would be to tag each of your views with something greater than zero and use that as your filter. Something along these lines should work:

[self.scrollVIew.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
UIView *view = obj;
if(view.tag > 0)
{
        NSLog(@"%@",NSStringFromCGRect(view.frame));
}
}];
Luke
  • 477
  • 4
  • 9
1

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));
}
}];
Pierre
  • 10,593
  • 5
  • 50
  • 80
  • Thanks it seems that a uiscrollview has 2 uiimageviews within it. Odd. Do you know why? – Dan Jul 04 '12 at 15:11
  • 1
    @Dan I actually wrote a blog post eons ago about why this is. They're actually the scroll bars. http://wiresareobsolete.com/wordpress/2010/03/uiscrollview-has-a-secret/ – devunwired Jan 14 '13 at 17:38