UIScrollView 常用属性 和 方法
常用属性
滚动内容属性
/// 内容偏移位置
@property(nonatomic) CGPoint contentOffset; // default CGPointZero
/// 内容大小,设置了才可以滚动
@property(nonatomic) CGSize contentSize; // default CGSizeZero
/// 内容间距
@property(nonatomic) UIEdgeInsets contentInset;
委托属性
@property(nullable,nonatomic,weak) id<UIScrollViewDelegate> delegate;
要想监听滚动视图的
滚动
/拖拽
/缩放
/状态栏交互
- 需要设置
delegate
- 实现相关协议方法
弹簧效果属性
/// 允许弹簧效果
@property(nonatomic) BOOL bounces; // default YES. if YES, bounces past edge of content and back again
/// 始终允许垂直弹
@property(nonatomic) BOOL alwaysBounceVertical; // default NO. if YES and bounces is YES, even if content is smaller than bounds, allow drag vertically
/// 始终允许水平弹
@property(nonatomic) BOOL alwaysBounceHorizontal;
分页属性
/// 允许分页
@property(nonatomic,getter=isPagingEnabled) BOOL pagingEnabled __TVOS_PROHIBITED;// default NO. if YES, stop on multiples of view bounds
指示器属性
/// 显示水平指示器
@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
/// 指示器间距
@property(nonatomic) UIEdgeInsets scrollIndicatorInsets; // default is UIEdgeInsetsZero. adjust indicators inside of insets
/// 指示器样式
@property(nonatomic) UIScrollViewIndicatorStyle indicatorStyle; // default is UIScrollViewIndicatorStyleDefault
在
scrollView
中指示器本质上就是UIImageView
通过查看视图层次结构可以看到
缩放属性
/// 最小缩放比例
@property(nonatomic) CGFloat minimumZoomScale; // default is 1.0
/// 最大缩放比例
@property(nonatomic) CGFloat maximumZoomScale; // default is 1.0. must be > minimum zoom scale to enable zooming
要允许缩放
- 必须设置以上两个属性
- 同时遵守协议
- 实现协议方法
-viewForZoomingInScrollView:
状态栏属性
/// 点击状态栏滚动到顶部
@property(nonatomic) BOOL scrollsToTop __TVOS_PROHIBITED; // default is YES.
一个视图中,如果有多个
scrollView
只有唯一一个
scrollView
的scrollsToTop
属性设置为 YES,才支持点击状态栏滚动到顶部
键盘属性
/// 键盘解除模式
@property(nonatomic) UIScrollViewKeyboardDismissMode keyboardDismissMode NS_AVAILABLE_IOS(7_0); // default is UIScrollViewKeyboardDismissModeNone
如果是
UITextView
,通常需要将alwaysBounceVertical
属性设置为YES
typedef NS_ENUM(NSInteger, UIScrollViewKeyboardDismissMode) {
// 无
UIScrollViewKeyboardDismissModeNone,
// 拖拽关闭键盘
UIScrollViewKeyboardDismissModeOnDrag, // dismisses the keyboard when a drag begins
// 必须要拖拽到键盘才可以关闭键盘,很少使用
UIScrollViewKeyboardDismissModeInteractive, // the keyboard follows the dragging touch off screen, and may be pulled upward again to cancel the dismiss
} NS_ENUM_AVAILABLE_IOS(7_0);
常用方法
/// 动画设置偏移位置
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated; // animate at constant velocity to new offset
/// 动画设置滚动区域
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated; // scroll so rect is just visible (nearest edges). nothing if rect completely visible