ios 如何禁用 UIScrollView 的水平滚动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1062693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:05:29  来源:igfitidea点击:

How to disable horizontal scrolling of UIScrollView?

iosobjective-cuiscrollview

提问by Rahul Vyas

I have a UIViewlike iPhone's Springboard. I have created it using a UIScrollViewand UIButtons. I want to disable horizontal scrolling on said scrollview. I want only vertical scrolling. How do I accomplish this?

我有一个UIView像 iPhone 的 Springboard。我已经使用 aUIScrollView和创建了它UIButtons。我想在所述滚动视图上禁用水平滚动。我只想要垂直滚动。我该如何实现?

回答by Dave DeLong

You have to set the contentSizeproperty of the UIScrollView. For example, if your UIScrollViewis 320 pixels wide (the width of the screen), then you could do this:

你要设置contentSize的属性UIScrollView。例如,如果您UIScrollView的宽度为 320 像素(屏幕的宽度),那么您可以这样做:

CGSize scrollableSize = CGSizeMake(320, myScrollableHeight);
[myScrollView setContentSize:scrollableSize];

The UIScrollViewwill then only scroll vertically, because it can already display everything horizontally.

UIScrollView会那么只有垂直滚动,因为它已经可以水平显示的一切。

回答by Dinesh Raja

UPDATED:(After @EranMarom pointed out on his comment)

更新:(在@EranMarom 指出他的评论之后)

You can stop horizontal scrolling or vertical scrollingin the ScrollViewDelegateMethod. Here it is how,

您可以在方法中停止水平滚动或垂直滚动ScrollViewDelegate。这是如何,

Stops Horizontal Scrolling:

停止水平滚动:

If you want to scroll horizontally, then you need to increase the contentOffset.x. Preventing that stops the scrollview scroll in horizontal direction.

如果要水平滚动,则需要增加 contentOffset.x。防止在水平方向上停止滚动视图滚动。

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    sender.contentOffset.x = 0.0
}

Stops Vertical Scrolling:

停止垂直滚动:

If you want to scroll vertically, then you need to increase the contentOffset.y. Preventing that stops the scrollview scroll in vertical direction.

如果要垂直滚动,则需要增加 contentOffset.y。防止在垂直方向上停止滚动视图滚动。

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    sender.contentOffset.y = 0.0
}

Above code prevents the changes in xand yof a scrollview contentOffsetand it leads to stop the scrolling in scrollViewDidScroll:method.

上面的代码阻止了a和a的更改,x并导致停止滚动 in方法。yscrollview contentOffsetscrollViewDidScroll:

回答by Mirko Brunner

since iOS7 use

由于iOS7使用

self.automaticallyAdjustsScrollViewInsets = NO;

//and create you page scroller with 3 pages

//并用3页创建你的页面滚动条

    self.pageView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.pageView setContentSize:CGSizeMake(self.view.frame.size.width*3, self.view.frame.size.height)];
    [self.pageView setShowsVerticalScrollIndicator:NO];
    [self.pageView setPagingEnabled:YES];
    [self.view addSubview:self.pageView];

回答by Dan Beaulieu

Swift solution

迅捷解决方案

Create two outlets, one for your view and one for your scroll view:

创建两个插座,一个用于您的视图,一个用于您的滚动视图:

@IBOutlet weak var myView: UIView!
@IBOutlet weak var scrollView: UIScrollView!

Then in your viewDidLayoutSubviews you can add the following code:

然后在您的 viewDidLayoutSubviews 中,您可以添加以下代码:

let scrollSize = CGSize(width: myView.frame.size.width, 
                        height: myView.frame.size.height)
scrollView.contentSize = scrollSize

What we've done is collected the height and width of the view and set the scrollViews content size to match it. This will stop your scrollview from scrolling horizontally.

我们所做的是收集视图的高度和宽度,并设置 scrollViews 内容大小以匹配它。这将阻止您的滚动视图水平滚动。



More Thoughts:

更多想法:

CGSizeMake takes a width & height using CGFloats. You may need to use your UIScrollViews existing height for the second parameter. Which would look like this:

CGSizeMake 使用 CGFloats 获取宽度和高度。您可能需要使用 UIScrollViews 现有高度作为第二个参数。看起来像这样:

let scrollSize = CGSize(width: myView.frame.size.width, 
                        height: scrollView.contentSize.height)

回答by Coder221

You can select the view, then under Attributes Inspectoruncheck User Interaction Enabled.

您可以选择视图,然后在Attributes Inspectoruncheck下User Interaction Enabled

回答by Gulfam Khan

In my case the width of the contentView was greater than the width of UIScrollView and that was the reason for unwanted horizontal scrolling. I solved it by setting the width of contentView equal to width of UIScrollView.

在我的情况下, contentView 的宽度大于 UIScrollView 的宽度,这就是不需要的水平滚动的原因。我通过将 contentView 的宽度设置为 UIScrollView 的宽度来解决它。

Hope it helps someone

希望它可以帮助某人

回答by Rincha

In my case, with Swift4.2 you can use:

就我而言,使用Swift4.2,您可以使用:

Disable vertical scroll:

禁用垂直滚动:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    scrollView.contentOffset.y = 0.0
}

Disable horizontal scroll:

禁用水平滚动:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    scrollView.contentOffset.x = 0.0
}

回答by iWheelBuy

Disable horizontal scrolling by overriding contentOffsetproperty in subclass.

通过覆盖contentOffset子类中的属性禁用水平滚动。

override var contentOffset: CGPoint {
  get {
    return super.contentOffset
  }
  set {
    super.contentOffset = CGPoint(x: 0, y: newValue.y)
  }
}

回答by PatriciaW

I struggled with this for some time trying unsuccessfully the various suggestions in this and other threads.

我为此苦苦挣扎了一段时间,但未能成功尝试此线程和其他线程中的各种建议。

However, in another thread (not sure where) someone suggested that using a negative constraint on the UIScrollView worked for him.

但是,在另一个线程(不确定在哪里)中,有人建议对 UIScrollView 使用负面约束对他有用。

So I tried various combinations of constraints with inconsistent results. What eventually worked for me was to add leading and trailing constraints of -32 to the scrollview and add an (invisible) textview with a width of 320 (and centered).

所以我尝试了各种约束组合,但结果不一致。最终对我有用的是向滚动视图添加 -32 的前导和尾随约束,并添加宽度为 320(并居中)的(不可见)文本视图。

回答by Naishta

I had the tableview contentInset set in viewDidLoad (as below) that what causing the horizontal scrolling

我在viewDidLoad(如下)中设置了tableview contentInset,导致水平滚动

self.tableView.contentInset = UIEdgeInsetsMake(0, 30, 0, 0);

Check if there are any tableview contentInset set for different reasons and disable it

检查是否有任何由于不同原因设置的 tableview contentInset 并禁用它