一.UIScrollView顯示或者隱藏滾動條, 默認有兩個屬性
@property(nonatomic) BOOL showsHorizontalScrollIndicator; // default YES. show indicator while we are tracking. fades out after tracking
@property(nonatomic) BOOL showsVerticalScrollIndicator; // default YES. show indicator while we are tracking. fades out after tracking
這兩個屬性默認是YES, 顯示水平和垂直滾動條, 只有當我們滾動的時候顯示,當我們不滾動時,慢慢淡出
二.UIScrollView滾動條的樣式, 默認有三個樣式
@property(nonatomic) UIScrollViewIndicatorStyle indicatorStyle; // default is UIScrollViewIndicatorStyleDefault
typedef NS_ENUM(NSInteger, UIScrollViewIndicatorStyle) {
UIScrollViewIndicatorStyleDefault, // black with white border. good against any background
UIScrollViewIndicatorStyleBlack, // black only. smaller. good against a white background
UIScrollViewIndicatorStyleWhite // white only. smaller. good against a black background
};
這個屬性默認是UIScrollViewIndicatorStyleDefault, 第二個屬性UIScrollViewIndicatorStyleBlack 滾動條是黑色, 第三個屬性UIScrollViewIndicatorStyleWhite 滾動條是白色
三.UIScrollView滾動條的偏移量
@property(nonatomic) UIEdgeInsets scrollIndicatorInsets; // default is UIEdgeInsetsZero. adjust indicators inside of insets
四.UIScrollView滾動條一直顯示
-
滾動條是一個UIImageView, 滾動條隱藏是因為設置了alpha屬性為0, 所有我們寫一個UIImageView的分類
#define noDisableVerticalScrollTag 836913 #define noDisableHorizontalScrollTag 836914 @implementation UIImageView (WLScrollView) - (void)setAlpha:(CGFloat)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
寫完分類之后, 我們在初始化UIScrollView的時候設置UIScrollView的tag為
836913
-
要在UIScrollView數(shù)據(jù)綁定之后, 調用以下方法
- (void)flashScrollIndicators; // displays the scroll indicators for a short time. This should be done whenever you bring the scroll view to front.
即調用以下方法
[scrollView reloadData]; [scrollView layoutIfNeeded]; // 第一次進來時沒有顯示出來由缆,可能是UI沒有更新注祖,添加這句猾蒂,等reloadData刷新完之后,更新UI是晨; [scrollView flashScrollIndicators];