How to disable vertical scrolling in UIScrollView (Obj-C)

I want to disable vertical scrolling from my UIScrollView, if possible. My code is similar to below. The work is excellent, except that users can scroll up and down, which should not be there, I believe .. Thanks in advance.

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]; 
+7
source share
7 answers

you must set the scroll content height to view the scroll height

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

In my situation, I was not able to get the scroll height (due to autostart, I could not get the height in viewDidLoad). You can add this to the delegate method.

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

There might be a duplicate here.

disable vertical scrolling in UIScrollView

or you can also try the following:

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

Assuming this is an iPhone app, therefore the screen resolution is 320x480 .

Now you set the scroll view height as self.view.frame.size.height / 3 . Here, the height of your view is actually taken as 460 , not 480 (20px for the staus bar).

Therefore, when you add another view as a subview in your scroll view, its frame leaves the view of the scroll content. Therefore, you need to control this when adjusting the frame / content size.

Let me know if this works for you.

0
source

There is no problem with this, just change the contentSize of your UIScrollView and you are done. Increase its width size and its height should be the same as it is now. In addition, you can also hide vertical scrollers.

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

You need to do the following:

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

Two scrolling properties are available in your xml file: horizontal scrolling and vertical scrolling. according to your requirement, you can check or uncheck, and if you want to stop vertical or horizontal scrolling, then you should make the same size of the scroll content with the height or width of the scrollview, respectively.

0
source

All Articles