禁用UIScrollView水平滚动的最佳方法是什么?

问题描述 投票:2回答:4

UIScrollView中,我的内容高度和宽度大于scrollview的大小。所以基本上它可以水平和垂直滚动。我想要的只是垂直滚动,我通过使用下面的代码使用UIButton水平滚动管理。

 [scrlViewQuestion setContentOffset:CGPointMake(questionWidth, 0) animated:YES];

防止水平滚动视图,可以设置scrollview内容宽度小于滚动视图大小但在我的情况下scrollview内容宽度大于其大小?那么解决这个问题的最佳方法是什么?

objective-c uiscrollview horizontal-scrolling contentsize
4个回答
3
投票

例如,内容UIView宽度应该等于UIScrollView的superview的宽度,而不是UIScrollView本身。

enter image description here


0
投票

将scrollview的contentSize设置为屏幕宽度和可滚动内容的实际高度。为contentInsettopbottom设置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)];

0
投票

您可以在故事板中执行此操作。

enter image description here

禁用Show Horizontal Indicator的Identity Inspector中的Scroll View选项卡。它将禁用水平滚动。


0
投票

swift 4.设置代表

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.x != 0 {
        scrollView.contentOffset.x = 0
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.