让滚动条在 UIScrollView 上始终可见?

Make scrollbar always visible on UIScrollView?(让滚动条在 UIScrollView 上始终可见?)

本文介绍了让滚动条在 UIScrollView 上始终可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 viewDidLoad 上创建一个始终可见的滚动条,以便用户可以了解有要滚动的内容.我做了以下事情:

[myscrollView flashScrollIndicators];

但随后滚动条仅在 viewDidLoad 后出现一段时间,然后再次消失,仅在用户触摸屏幕时重新出现..

我需要让滚动条始终可见.我该怎么做?

解决方案

Apple 间接不鼓励在其

  • UIScrollView 指示器总是显示?
  • 滚动指示器可见性
  • 使滚动条在 uiscrollview 中始终可见
  • I need to make a scrollbar always visible on viewDidLoad so that the user can understand that there is content to scroll. I did the following:

    [myscrollView flashScrollIndicators];
    

    But then the scrollbars only appear for some time after viewDidLoad and disappear again only to reappear when the user touches the screen..

    I need to make scrollbars always visible. How can I do it?

    解决方案

    Apple indirectly discourage constantly displaying scroll indicators in their iOS Human Interface Guidelines but guidelines are just guidelines for a reason, they don't account for every scenario and sometimes you may need to politely ignore them.

    The scroll indicators of any content views are UIImageView subviews of those content views. This means you can access the scroll indicators of a UIScrollView as you would any of its other subviews (i.e. myScrollView.subviews) and modify the scroll indicators as you would any UIImageView (e.g. scrollIndicatorImageView.backgroundColor = [UIColor redColor];).

    The most popular solution appears to be the following code:

    #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
    

    Which is originally credited to this source.

    This defines a category for UIImageView that defines a custom setter for the alpha property. This works because at some point in the underlying code for the UIScrollView, it will set its scroll indicator's alpha property to 0 in order to hide it. At this point it will run through our category and, if the hosting UIScrollView has the right tag, it will ignore the value being set, leaving it displayed.

    In order to use this solution ensure your UIScrollView has the appropriate tag e.g.

    If you want to display the scroll indicator from the moment its UIScrollView is visible simply flash the scroll indicators when the view appears .e.g

    - (void)viewDidAppear:(BOOL)animate
    {
        [super viewDidAppear:animate];
        [self.scrollView flashScrollIndicators];
    }
    

    Additional SO references:

    • UIScrollView - showing the scroll bar
    • UIScrollView indicator always show?
    • Scroll Indicators Visibility
    • Make scrollbars always visible in uiscrollview

    这篇关于让滚动条在 UIScrollView 上始终可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

    本文标题为:让滚动条在 UIScrollView 上始终可见?

    基础教程推荐