7

I want to disable vertical scrolling from my UIScrollView if possible.. My code is like below.. Working fine except users can scroll up and down which shouldn't 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];
Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99
user123
  • 2,711
  • 5
  • 21
  • 25
  • 1
    possible duplicate of http://stackoverflow.com/questions/5095713/disabling-vertical-scrolling-in-uiscrollview – Eyal Aug 07 '12 at 11:07
  • yes i used the same thing but didn't work for me!! – user123 Aug 07 '12 at 11:09
  • @NayanChauhan it's tagged ios. – The dude Aug 07 '12 at 11:22
  • I took your code and place it in viewDidLoad in a new project and I got no scroll at all... Try setting the scrollView contentSize in the end of this method. – Eyal Aug 07 '12 at 11:24
  • just set the `contentSize`. Trust me , it works. The only reason why it wouldn't work would be if you set the contentSize like this `CGSizeMake(yourDesiredWidth , 0 ) ; //which is correct` and THEN set it's contentSize with a height larger than the scrollView's frame height. – George Aug 07 '12 at 11:25
  • i meant is it iPhone or iPad app? – Nayan Chauhan Aug 07 '12 at 11:28
  • there is no problem with that simply change the contentSize of your UIScrollView and you are done.Increase its width size and its height should be as it is at present.Moreover you can also hide the vertical scrollers also. – AJS Aug 07 '12 at 11:35
  • Just decrease the height value in contentSize property of scrollview to scrollview height.And this will work. – Deepak Srivastava May 21 '13 at 07:50

7 Answers7

22

In my situation, I was unable to get the height of the scrollview (due to autolayout, I wasn't able to 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);
}
Gavy
  • 1,939
  • 1
  • 19
  • 20
10

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

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

touti
  • 1,164
  • 6
  • 18
4

Here may be a possible duplicate

disabling vertical scrolling in UIScrollView

or you can also try this:

self.scrollview.contentSize = CGSizeMake(self.scrollview.frame.size.width * number_of_items, 1);
Community
  • 1
  • 1
Nayan
  • 3,014
  • 2
  • 17
  • 33
1

Assuming it's an iPhone app, so the screen resolution is 320×480 .

Now you are setting your scroll view's height as self.view.frame.size.height / 3 . Here your view's height is actually taken as 460 and not 480 (20px for staus bar).

So when you add the other view as subview to your scroll view, its frame goes out of the scroll's content view. So you need to manage this while setting your frames/content size.

Let me know if this works for you.

Nayan Chauhan
  • 542
  • 5
  • 16
0

There is no problem with that simply change the contentSize of your UIScrollView and you are done.Increase its width size and its height should be as it is at present.Moreover you can also hide the vertical scrollers also.

scroll.showsVerticalScrollIndicator = NO;
scroll.contentSize = CGSizeMake(scroll.contentSize.width + xVal,scroll.frame.size.height); 
AJS
  • 1,403
  • 12
  • 17
0

You should do like this:

aScrollView.scrollsToTop = NO;
aScrollView.delegate = self;
aScrollView.contentSize = CGSizeMake(aScrollView.frame.size.width * X, aScrollView.frame.size.height/2);
Paradise
  • 558
  • 6
  • 17
0

In your xml file there are two properties are available for scrollview are horizontal scroll and vertical scroll. as per your requirement you can check or uncheck and if you want to stop vertical or horizontal scroll then you have to make same content size of scrollview with height or width of scrollview respectively