如何在UIScrollView(Obj-C)中禁用垂直滚动

我想从我的UIScrollView禁用垂直滚动,如果可能的话..我的代码就像下面..工作正常,除了用户可以上下滚动应该不会在那里我相信..在此先感谢..

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height / 3)]; scroll.contentSize = CGSizeMake(scroll.contentSize.width,scroll.frame.size.height); scroll.pagingEnabled = YES; scroll.backgroundColor = [UIColor blackColor]; int xVal = 30; NSInteger numberOfViews = 5; for (int i = 0; i < numberOfViews; i++) { UILabel *testLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(xVal, 0, 90, 100)]; UILabel *testLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(xVal, 20, 90, 100)]; UILabel *testLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(xVal, 40, 90, 100)]; testLabel2.backgroundColor = [UIColor clearColor]; testLabel2.text =@"Test1"; testLabel2.textColor = [UIColor whiteColor]; testLabel2.font = [UIFont boldSystemFontOfSize:12]; testLabel1.backgroundColor = [UIColor clearColor]; testLabel1.text =@"Test2"; testLabel1.textColor = [UIColor whiteColor]; testLabel1.font = [UIFont boldSystemFontOfSize:12]; testLabel3.backgroundColor = [UIColor clearColor]; testLabel3.text =@"Test3"; testLabel3.textColor = [UIColor whiteColor]; testLabel3.font = [UIFont boldSystemFontOfSize:12]; xVal += 120; UIView *view = [[UIView alloc] initWithFrame:CGRectMake(xVal, 30, 150, 130)]; view.backgroundColor = [UIColor blackColor]; xVal += 200; [scroll addSubview:testLabel1]; [scroll addSubview:testLabel2]; [scroll addSubview:testLabel3]; [scroll addSubview:view]; } [self.view addSubview:scroll]; 

您必须将您的滚动视图内容高度设置为滚动视图高度

 CGSize scrollableSize = CGSizeMake(scrollableWidth,yourScrollViewHeight);
 [myScrollView setContentSize:scrollableSize];

在我的情况下,我无法得到滚动视图的高度(由于自动布局,我无法得到viewDidLoad高度)。 您可以将其添加到委托方法。

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0); } 

这可能是一个可能的重复

在UIScrollView中禁用垂直滚动

或者你也可以试试这个:

 self.scrollview.contentSize = CGSizeMake(self.scrollview.frame.size.width * number_of_items, 1); 

假设这是一个iPhone应用程序,所以屏幕分辨率是320×480

现在,您将滚动视图的高度设置为self.view.frame.size.height / 3 。 在这里你的视图的高度实际上是460而不是480 (20px为staus酒吧)。

因此,当您将其他视图作为子视图添加到滚动视图时,其框架将不在滚动的内容视图中。 所以你需要在设置帧/内容大小时进行pipe理。

让我知道这是否适合你。

这是没有问题的,只是简单地改变你的UIScrollView的contentSize,你就完成了。增加它的宽度大小,它的高度应该是现在的样子。而且你也可以隐藏垂直滚动条。

 scroll.showsVerticalScrollIndicator = NO; scroll.contentSize = CGSizeMake(scroll.contentSize.width + xVal,scroll.frame.size.height); 

你应该这样做:

 aScrollView.scrollsToTop = NO; aScrollView.delegate = self; aScrollView.contentSize = CGSizeMake(aScrollView.frame.size.width * X, aScrollView.frame.size.height/2); 

在你的XML文件有两个属性可用于滚动查看是水平滚动和垂直滚动。 根据您的要求,你可以select或取消选中,如果你想停止垂直或水平滚动,那么你必须使scrollview的高度或宽度分别相同的内容大小