最近碰到一个需求,让scrollView内容过多时滚动条一直显示,通过上网查阅还有自己的调试:方法如下:
1.首先创建一个分类:
#define noDisableVerticalScrollTag 836913
#define noDisableHorizontalScrollTag 836914
//这个方法是为了让scrollView的滚动条一直存在
@implementation UIImageView (JJScrollView)
- (void)setAlpha:(CGFloat)alpha
{
DebugLog(@"%ld",self.superview.tag);
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];
}
2.若希望竖直方向的滚动条一直存在,设置scrollView.tag = noDisableVerticalScrollTag.
若希望水平方向的滚动条一直存在,设置scrollView.tag = noDisableHorizontalScrollTag
3.先确保scrollVIew的宽高和contentsize(本人用masonry布局,所以一般会先加[ScrollView layoutifneed]方法),如果scrollView中是用label自适应撑开的话,还需要在layoutifneed方法调用前加上[label sizeToFit]方法.
4.调用[scrollView flashScrollIndicators];
最后实现结果:scrollView的cotentSize>size时,滚动条一直存在,contentsize<size时,滚动条不显示
网友评论