views:

898

answers:

3

I have a UIScrollView which I create and size dynamically using...

scrollView.contentSize = CGSizeMake(scrollView.frame.size.width , length);

I then add subviews to the UIScrollView. I do have scrollView.showsVerticalScrollIndicator = YES;

When scrolling the scroll indicator never appears.

Even if I call [scrollView flashScrollIndicators] nothing happens.

Ideas?

A: 

Noticed this when the UIScrollView was a 48 px tall horizontal band, scrollable horizontally. Maybe Cocoa decides the area is too small for a scroll indicator...

Seva Alekseyev
In this case the scrollview is several times the length of the screen and exactly the width of the screen.
George
+1  A: 

When I've dealt with this before, in my implementation of a grid, I would occasionally get some cells over the top of the scroll indicator. To fix this I am now inserting subviews at index 0 rather than adding them, which adds them to the top. So try something like this:

[scrollview insertSubview:subview atIndex:0];

Daniel Tull
+3  A: 

Had the same problem and couldn't find the cause. The last answer gave the final hint: whenever I added new subviews I first removed all existing subviews from the scrollview (and apparently also the scroll indicators).

After checking in my loop, if the subview really was of the kind I wanted to be removed the scroll indicators showed up:

for (NSObject * subview in [[[scrollView subviews] copy] autorelease]) {
    if ([subview isKindOfClass:[MySubView class]]) {
       [(MySubView*)subview removeFromSuperview];
    }
}   

Update: changed code to Nikolai's suggestion

Pegolon
There's a subtle bug in this code: UIView's `subviews` method returns the original array of subviews used by the view. This means the above code mutates the array while iterating over it. Easy fix: iterate over `[[[scrollView subviews] copy] autorelease]`
Nikolai Ruhe