UIScrollView – 显示滚动条

可能是一个简单的!

有谁知道如何让UIScrollView的滚动条不断显示?

它在用户滚动时显示,所以他们可以看到他们在滚动视图的位置。

但我希望它不断显示,因为它不是立即显示给用户滚动是可用的

任何意见将不胜感激。

不,你不能让他们一直显示,但你可以让他们暂时闪光。

[myScrollView flashScrollIndicators]; 

他们是滚动指标,而不是滚动条。 你不能用它们滚动。

我的解决scheme ,一直显示滚动指标

 #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 

更新 :此解决scheme在64位上导致一些问题。 更多细节请看这里

据我所知,这是不可能的。 控制显示滚动指示器的唯一API调用是showsVerticalScrollIndicator ,只能禁用显示指示器。

当视图出现时,您可以使用flashScrollIndicators ,以便用户知道它们在滚动视图中的位置。

这个为我工作:

 #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 

我从这里得到这个片段: http : //www.developers-life.com/scrollview-with-scrolls-indicators-which-are-shown-all-the-time.html

对于第一个子视图是滚动视图的网页浏览,在最新的SDK中,如果HTML页面比框架长,则不显示滚动条,如果html内容碰巧与框架alignment,或者您有空白在框架的底部,它看起来像没有滚动需要,没有任何东西在线以下。 在这种情况下,我认为你应该明确地闪烁代表的滚动条

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

方法来提醒用户,“盒子外面”有更多的东西。

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

使用HTML时,水平内容会自动换行,所以请检查webscroller的高度。

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

闪光灯是如此之短,发生在视图加载时,它可以被忽略。 要解决这个问题,你也可以通过通用的UIView commitAnimations

iOS不提供该API。 但是,如果你真的想要这个,你可以添加自定义指标来自己滚动视图和布局,就像演示一样:

 - (void)layoutSubviews
 {
     [super layoutSubviews];

     if(self.showsVerticalScrollIndicatorAlways){
         scroll_indicator_position(self,k_scroll_indicator_vertical);
     }

     if(self.showsHorizo​​ntalScrollIndicatorAlways){
         scroll_indicator_position(self,k_scroll_indicator_horizo​​ntal);
     }
 }

链接是https://github.com/flexih/MazeScrollView

ScrollBar的function与iOS中内置的一样,但是可以混合颜色和宽度。

 -(void)persistantScrollBar { [persistantScrollBar removeFromSuperview]; [self.collectionView setNeedsLayout]; [self.collectionView layoutIfNeeded]; if (self.collectionView.contentSize.height > self.collectionView.frame.size.height + 10) { persistantScrollBar = [[UIView alloc] initWithFrame:(CGRectMake(self.view.frame.size.width - 10, self.collectionView.frame.origin.y, 5, (self.collectionView.frame.size.height /self.collectionView.contentSize.height) * self.collectionView.frame.size.height))]; persistantScrollBar.backgroundColor = [UIColor colorWithRed:207/255.f green:207/255.f blue:207/255.f alpha:0.5f]; persistantScrollBar.layer.cornerRadius = persistantScrollBar.frame.size.width/2; persistantScrollBar.layer.zPosition = 0; [self.view addSubview:persistantScrollBar]; } } -(void)scrollViewDidScroll:(UIScrollView *)scrollView { CGRect rect = persistantScrollBar.frame; rect.origin.y = scrollView.frame.origin.y + (scrollView.contentOffset.y *(self.collectionView.frame.size.height/self.collectionView.contentSize.height)); rect.size.height = (self.collectionView.frame.size.height /self.collectionView.contentSize.height) * self.collectionView.frame.size.height; if ( scrollView.contentOffset.y <= 0 ) { rect.origin.y = scrollView.frame.origin.y; rect.size.height = rect.size.height + (scrollView.contentOffset.y); } else if (scrollView.contentOffset.y + scrollView.frame.size.height >= scrollView.contentSize.height) { rect.size.height = rect.size.height - ((scrollView.contentOffset.y + scrollView.frame.size.height) - scrollView.contentSize.height); rect.origin.y = (self.collectionView.frame.origin.y + self.collectionView.frame.size.height - 5) - rect.size.height; } persistantScrollBar.frame = rect; } 

我想提供我的解决scheme。 我不喜欢最stream行的变体类别(在类别中重写方法可能是一些不确定的原因,应该在运行时调用什么方法,因为有两个方法具有相同的select器)。 我使用调合。 而且我也不需要使用标签。

添加这个方法到你的视图控制器,你有滚动视图( self.categoriesTableView属性是一个表格视图,我想显示滚动条)

 - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // Do swizzling to turn scroll indicator always on // Search correct subview with 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; } } } } } // Ask to flash indicator to turn it on [self.categoriesTableView flashScrollIndicators]; } 

添加新课程

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

滚动指示器(这种情况下的垂直滚动指示器)将始终在屏幕上。

Swift 3

您可以使用scrollView.subviews访问滚动条,并修改alpha,如下所示。 这个对我有用。

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