UIScrollView - showing the scroll bar

前端 未结 10 1717
-上瘾入骨i
-上瘾入骨i 2020-11-27 20:32

Possibly a simple one!

Does anyone know how to get the scroll bar of a UIScrollView to constantly show?

It displays when the user is scrolling, so they can s

相关标签:
10条回答
  • 2020-11-27 21:23

    iOS does not offer the API. But if you really want this, you can add your custom indicator to scroll view and layout it yourself, just as the demo does:

    - (void)layoutSubviews
    {
        [super layoutSubviews];
    
        if (self.showsVerticalScrollIndicatorAlways) {
            scroll_indicator_position(self, k_scroll_indicator_vertical);
        }
    
        if (self.showsHorizontalScrollIndicatorAlways) {
            scroll_indicator_position(self, k_scroll_indicator_horizontal);
        }
    }
    

    The link is https://github.com/flexih/MazeScrollView

    0 讨论(0)
  • 2020-11-27 21:25

    For webviews, where the first subview is a scrollview, in the latest SDK, if an HTML page is longer than the frame, no scroll bar is shown, and if the html content happens to line up with the frame, or you have a whitespace at the bottom of the frame, it 'looks' like there is no scroll needed and nothing below the line. In this case, I think you should definately flash the scroll bars in the delegate's

    - (void)webViewDidFinishLoad:(UIWebView *)webView; 
    

    method to alert the user that there is more stuff 'outside the box'.

    NSArray *subViews = [[NSArray alloc] initWithArray:[webView subviews]] ;
    UIScrollView *webScroller = (UIScrollView *)[subViews objectAtIndex:0] ;    
    

    With HTML, the horizontal content is wrapped automatically, so check the webscroller height.

            if (webScroller.contentSize.height > webView.frame.size.height) {
                [webScroller flashScrollIndicators];
            }
    

    The flash is so short, and happens while over views are loading, that it can be overlooked. To work around that, you could also jiggle or bounce or scroll or scale the content a little via the generic UIView commitAnimations

    0 讨论(0)
  • 2020-11-27 21:28

    This one worked for me:

    #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
    

    I got this snippet from here: http://www.developers-life.com/scrollview-with-scrolls-indicators-which-are-shown-all-the-time.html

    0 讨论(0)
  • 2020-11-27 21:29

    Swift 3

    You can access the scrollbar using scrollView.subviews and modify the alpha as shown here. It works for me.

    extension UIScrollView {
        override open func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
            for x in self.subviews {
                x.alpha = 1.0
            }
        }
    }
    
    extension MyScrollViewDelegate : UIScrollViewDelegate {
        func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
            for x in scrollView.subviews {
                x.alpha = 1.0
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题