0

I am going to add a UIScrollView into a UIView programmatically. I tried to use the following code for this but the scrollview is still not disabled.

-(id)initWithFrame:(CGRect)frame {
...
  _scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
  [self addSubview:_scrollView];
...
}
-(void)layoutSubviews {
...
  _scrollView.frame = CGRectMake(0, 0, self.frame.size.width, 40);
  [_scrollView setContentSize:CGSizeMake(self.frame.size.width * 2, 40)];
...
}

I think it should work but it's not working now. Please advise me what the problem is. Thanks

nine9stars
  • 247
  • 3
  • 12

6 Answers6

2

Well, try to set the contentSize like this:

[_scrollView setContentSize:CGSizeMake(self.frame.size.width * 2, 0)];

Let me know the result. Thanks.

Chris Forever
  • 678
  • 1
  • 5
  • 18
0

Try setting _scrollView.scrollEnabled = NO; This will disable scrollView to scroll vertically or horizontally.

UPDATE: To avoid vertical scrolling only

Refer this link

You need to set frame of your scrollView and then set its contentSize

_scrollView.contentSize = CGSizeMake(scrollView.contentSize.width,scrollView.frame.size.height);
Community
  • 1
  • 1
Sushil Sharma
  • 2,321
  • 3
  • 29
  • 49
  • it disables both vertical and horizontal scrolling at the same time. I need to enable only the horizontal scrolling. – nine9stars Oct 10 '15 at 04:09
  • in my question, both the height of scrollview frame and contentsize are currently 40. But it doesn't work. – nine9stars Oct 11 '15 at 18:04
0

You can try below code:

For Objective - C

CGFloat y = SCROLLVIEW_HEIGHT;

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

In Swift

self.scrollView.contentSize.height = 1.0 
Hardik Shekhat
  • 1,680
  • 12
  • 21
0

For this you must set - your scrollview content height is the scroll view height

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

Also the another way is

If the scrollView's contentSize.height is less than its bounds.size.height it won't scroll vertically.

You need to set

 scrollView.contentSize = (CGSize){<yourContentWidth>, 1.0}
user3182143
  • 9,459
  • 3
  • 32
  • 39
0

You need only Horizontal scrolling, vertical scrolling is not needed. Try the code below. I don't know where you stuck:

(void)viewDidLoad {
    [super viewDidLoad];
    [self CreateScrollView];
}

(void)CreateScrollView 
{
   UIScrollView *Scroll_View = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 40)];
  [self addSubview:Scroll_View];
  [_scrollView setContentSize:CGSizeMake(self.frame.size.width * 2, 40)];    
}
ᗩИᎠЯƎᗩ
  • 2,122
  • 5
  • 29
  • 41
-1

You can try like this:

[_scrollView setShowsVerticalScrollIndicator:NO];

P.Chen
  • 1
  • 2