nine9stars
nine9stars

Reputation: 247

Objective-C: How to disable verticall scrolling in UIScrollView

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

Upvotes: 0

Views: 944

Answers (6)

Hardik Shekhat
Hardik Shekhat

Reputation: 1888

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 

Upvotes: 0

ManiVasanth
ManiVasanth

Reputation: 86

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)];    
}

Upvotes: 0

user3182143
user3182143

Reputation: 9609

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}

Upvotes: 0

P.Chen
P.Chen

Reputation: 1

You can try like this:

[_scrollView setShowsVerticalScrollIndicator:NO];

Upvotes: -1

Chris Forever
Chris Forever

Reputation: 678

Well, try to set the contentSize like this:

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

Let me know the result. Thanks.

Upvotes: 2

Sushil Sharma
Sushil Sharma

Reputation: 2361

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);

Upvotes: 0

Related Questions