Make scrollbar always visible on UIScrollView?(让滚动条在 UIScrollView 上始终可见?)
问题描述
我需要在 viewDidLoad 上创建一个始终可见的滚动条,以便用户可以了解有要滚动的内容.我做了以下事情:
[myscrollView flashScrollIndicators];
但随后滚动条仅在 viewDidLoad 后出现一段时间,然后再次消失,仅在用户触摸屏幕时重新出现..
我需要让滚动条始终可见.我该怎么做?
Apple 间接不鼓励在其
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 上始终可见?
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01