UIScrollView not showing scroll indicator

2019-02-01 07:33发布

问题:

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?

回答1:

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



回答2:

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];



回答3:

For me, the horizontal indicator had mysteriously disappeared in my app on iOS 7. Later found out that for some strange reason, I had to enable both Shows Horizontal Indicator and Shows Vertical Indicator to make the horizontal one show up. If I set it to not show the vertical indicator, it would also not show horizontal indicator.



回答4:

I fix this by adding this code after add new subview:

self.showsVerticalScrollIndicator = NO;
self.showsVerticalScrollIndicator = YES;


回答5:

It can happen also if the parent of the scrollview is smaller horizontally than the scroll view itself :

The scroll bar is stuck to the right side of the ScrollView / TableView and this right side is not visible due to the parent bounds ( with a clipToBounds hidding it for instance).

I've seen this issue so I share it in case it can help.

Just check the width of your ScrollView's frame not to be bigger than the width of its parent view frame.



回答6:

It will also happen (at least in the case of a UITableView) if the contentSize is too small for the table view to scroll. If you have enabled bouncing, then the tableview does not actually scroll and does not display the indicators therefore. Try fitting more content inside.



回答7:

Two conditions,

  1. If you are using a storyboard
  2. If you are using a UITableView inside a UIViewController

Then, you should check your indicator insets are set to 0 (or any other number that is relevant to your autolayout):



回答8:

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...