ios 在 UIScrollView 中禁用垂直滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5095713/
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 18:59:43  来源:igfitidea点击:

Disabling vertical scrolling in UIScrollView

iphoneiosuiscrollview

提问by system

There is an option in IB to uncheck vertical scrolling on a scrollview, but it doesnt seem to work.

IB 中有一个选项可以取消选中滚动视图上的垂直滚动,但它似乎不起作用。

How can the scrollview be set to only scroll horizontally, and not vertically in the code rather than IB?

如何在代码中将滚动视图设置为仅水平滚动,而不是垂直滚动而不是 IB?

回答by Mirko Brunner

since iOS7:

从 iOS7 开始:

first: the content size width must be equal to the width of your scrollview

第一:内容大小宽度必须等于滚动视图的宽度

second: in your initWithNibName:

第二:在你的 initWithNibName 中:

self.automaticallyAdjustsScrollViewInsets = NO;

That′s it.

就是这样。

回答by pt2ph8

Try setting the contentSize's height to the scrollView's height. Then the vertical scroll should be disabled because there would be nothing to scroll vertically.

尝试将 contentSize 的高度设置为 scrollView 的高度。然后应该禁用垂直滚动,因为垂直滚动没有任何内容。

scrollView.contentSize = CGSizeMake(scrollView.contentSize.width,scrollView.frame.size.height);

回答by meronix

yes, pt2ph8's answer is right,

是的,pt2ph8的答案是对的,

but if for some strange reason your contentSize should be higher than the UIScrollView, you can disable the vertical scrolling implementing the UIScrollView protocol method

但是如果由于某种奇怪的原因你的 contentSize 应该高于 UIScrollView,你可以禁用实现 UIScrollView 协议方法的垂直滚动

 -(void)scrollViewDidScroll:(UIScrollView *)aScrollView;

just add this in your UIViewController

只需在您的 UIViewController 中添加它

float oldY; // here or better in .h interface

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
    [aScrollView setContentOffset: CGPointMake(aScrollView.contentOffset.x, oldY)];
    // or if you are sure you wanna it always on top:
    // [aScrollView setContentOffset: CGPointMake(aScrollView.contentOffset.x, 0)];
}

it's just the method called when the user scroll your UIScrollView, and doing so you force the content of it to have always the same .y

它只是用户滚动 UIScrollView 时调用的方法,这样做会强制它的内容始终具有相同的 .y

回答by Febin P

Include the following method

包括以下方法

-(void)viewDidLayoutSubviews{
    self.automaticallyAdjustsScrollViewInsets = NO;
}

and set the content size width of the scroll view equal to the width of the scroll view.

并将滚动视图的内容大小宽度设置为等于滚动视图的宽度。

回答by Mani

You need to pass 0 in content size to disable in which direction you want.

您需要在内容大小中传递 0 以禁用您想要的方向。

To disable vertical scrolling

禁用垂直滚动

scrollView.contentSize = CGSizeMake(scrollView.contentSize.width,0);

To disable horizontal scrolling

禁用水平滚动

scrollView.contentSize = CGSizeMake(0,scrollView.contentSize.height);

回答by bolonn

I updated the content size to disable vertical scrolling, and the ability to scroll still remained. Then I figured out that I needed to disable vertical bounce too, to disable completly the scroll.

我更新了内容大小以禁用垂直滚动,并且滚动的能力仍然存在。然后我发现我也需要禁用垂直反弹,以完全禁用滚动。

Maybe there are people with this problem too.

也许也有人有这个问题。

回答by bolonn

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
    [aScrollView setContentOffset: CGPointMake(aScrollView.contentOffset.x,0)];

}

you must have confirmed to UIScrollViewDelegate

你必须确认 UIScrollViewDelegate

aScrollView.delegate = self;

回答by Michael

On iOS 11 please remember to add the following, if you're interested in creating a scrollview that sticks to the screen bounds rather than a safe area.:

在 iOS 11 上,如果您有兴趣创建一个粘在屏幕边界而不是安全区域的滚动视图,请记住添加以下内容。:

if (@available(iOS 11.0, *)) {
    [self.scrollView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
}

回答by manuelsantos

Just set the y to be always on top. Need to conform with UIScrollViewDelegate

只需将 y 设置为始终在顶部。需要符合 UIScrollViewDelegate

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

This will keep the Deceleration / Acceleration effect of the scrolling.

这将保持滚动的减速/加速效果。

回答by malex

From iOS11one can use the following property

iOS11可以使用以下属性

let frameLayoutGuide: UILayoutGuide

If you set constraints for frameLayoutGuide.topAnchorand frameLayoutGuide.bottomAnchorto the same anchors of some subviewof your scrollViewthen vertical scroll will be disabled and the height of the scrollViewwill be equal to the height of its subview.

如果您设置的限制frameLayoutGuide.topAnchorframeLayoutGuide.bottomAnchor到一些相同的锚子视图您的滚动视图,然后垂直滚动将被禁用和高度滚动视图将等于其子视图的高度。