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 上始终可见?


- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01