简体   繁体   中英

Make scrollbar always visible on UIScrollView (iOS7)?

Its possible to make UIScrollView indicator always show in iOS7? and not only when scrolling!

I've searched and cannot find a way to make it work.

I've tried the code below but it does not work on iOS7

#define noDisableVerticalScrollTag 836913
#define noDisableHorizontalScrollTag 836914

@implementation UIImageView (ForScrollView) 

- (void) setAlpha:(float)alpha {

    if (self.superview.tag == noDisableVerticalScrollTag) {
        if (alpha == 0 && self.autoresizingMask == UIViewAutoresizingFlexibleLeftMargin) {
            if (self.frame.size.width < 10 && self.frame.size.height > self.frame.size.width) {
                UIScrollView *sc = (UIScrollView*)self.superview;
                if (sc.frame.size.height < sc.contentSize.height) {
                    return;
                }
            }
        }
    }

    if (self.superview.tag == noDisableHorizontalScrollTag) {
        if (alpha == 0 && self.autoresizingMask == UIViewAutoresizingFlexibleTopMargin) {
            if (self.frame.size.height < 10 && self.frame.size.height < self.frame.size.width) {
                UIScrollView *sc = (UIScrollView*)self.superview;
                if (sc.frame.size.width < sc.contentSize.width) {
                    return;
                }
            }
        }
    }

    [super setAlpha:alpha];
}

@end

The code you've posted seems to be a category that overrides setAlpha on UIImageView , and checks the view's tag to see if it matches the expected tag for the UIScrollView indicator bars. This is really hacky. You're relying on the tags remaining the same (which they invariably won't), and the indicator bars being UIImageView objects (which they may be now, but you have no guarantee of this in future iOS releases).

You cannot set the scroll bar indicators in a UIScrollView to be permanently on - it's not supported in the API. Rather than try to mess around with the underlying architecture of the scroll view, if you really wanted to do this you would probably be better off implementing your own indicators.

This shouldn't be too difficult, since you know the size of the content in the scroll view, and you can also obtain the offset, so it's just a matter of creating two indicator views and moving them around as required (I say 'too difficult' - it's not easy, but it's a marginally more elegant solution than the code you've posted).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM