UIScrollView - showing the scroll bar

前端 未结 10 1748
-上瘾入骨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:13

    I want to offer my solution. I don't like the most popular variant with category (overriding methods in category can be the reason of some indetermination what method should be called in runtime, since there is two methods with the same selector). I use swizzling instead. And also I don't need to use tags.

    Add this method to your view controller, where you have scroll view (self.categoriesTableView property is a table view where I want to show scroll bars)

    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        // Do swizzling to turn scroll indicator always on
        // Search correct subview with vertical scroll indicator image across tableView subviews
        for (UIView * view in self.categoriesTableView.subviews) {
            if ([view isKindOfClass:[UIImageView class]]) {
                if (view.alpha == 0 && view.autoresizingMask == UIViewAutoresizingFlexibleLeftMargin) {
                    if (view.frame.size.width < 10 && view.frame.size.height > view.frame.size.width) {
                        if (self.categoriesTableView.frame.size.height < self.categoriesTableView.contentSize.height) {
                            // Swizzle class for found imageView, that should be scroll indicator
                            object_setClass(view, [AlwaysOpaqueImageView class]);
                            break;
                        }
                    }
                }
            }
        }
        // Search correct subview with horizontal scroll indicator image across tableView subviews
        for (UIView * view in self.categoriesTableView.subviews) {
            if ([view isKindOfClass:[UIImageView class]]) {
                if (view.alpha == 0 && view.autoresizingMask == UIViewAutoresizingFlexibleTopMargin) {
                    if (view.frame.size.height < 10 && view.frame.size.height < view.frame.size.width) {
                        if (self.categoriesTableView.frame.size.width < self.categoriesTableView.contentSize.width) {
                            // Swizzle class for found imageView, that should be scroll indicator
                            object_setClass(view, [AlwaysOpaqueImageView class]);
                            break;
                        }
                    }
                }
            }
        }
        // Ask to flash indicator to turn it on
       [self.categoriesTableView flashScrollIndicators];
    }
    

    Add new class

    @interface AlwaysOpaqueImageView : UIImageView
    @end
    
    @implementation AlwaysOpaqueImageView
    
    - (void)setAlpha:(CGFloat)alpha {
        [super setAlpha:1.0];
    }
    
    @end
    

    The scroll indicator (vertical scroll indicator in first for cycle and horizontal in second for cycle) will be always at the screen. If you need only one indicator, left only this for cycle in code and remove another one.

提交回复
热议问题