在UIScrollView
中,我的内容高度和宽度大于scrollview的大小。所以基本上它可以水平和垂直滚动。我想要的只是垂直滚动,我通过使用下面的代码使用UIButton
水平滚动管理。
[scrlViewQuestion setContentOffset:CGPointMake(questionWidth, 0) animated:YES];
防止水平滚动视图,可以设置scrollview内容宽度小于滚动视图大小但在我的情况下scrollview内容宽度大于其大小?那么解决这个问题的最佳方法是什么?
将scrollview的contentSize
设置为屏幕宽度和可滚动内容的实际高度。为contentInset
,top
和bottom
设置right
为零。对于left
上的内容插入,请始终选择当前要显示的位置的否定x
坐标。
例如。如果您的屏幕宽度为320点,并且您希望显示固定在x
的水平滚动内容,但允许垂直滚动:
CGFloat screenWidth = 320.0;
CGSize actualContentSize = CGSizeMake(3000, 3000);
[scrlViewQuestion setContentSize:CGSizeMake(screenWidth, actualContentSize.height)];
[scrlViewQuestion setContentInset:UIEdgeInsetsMake(0, -x, 0, 0)];
swift 4.设置代表
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.x != 0 {
scrollView.contentOffset.x = 0
}
}