Reputation: 11696
I have a view controller with a button. When the button is pressed, I want to add a subview which contains a scrollview. Right now my code displays the subview and scrolls fine as long as I don't add the xib. When I add the xib to the scrollview, the xib appears in the subview but there is no longer any scrolling.
The main View Controller:
-(IBAction)startNewSearch:(id)sender {
UIScrollView *myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 600)];
UIView *myView = [[[NSBundle mainBundle] loadNibNamed:@"NewSearch" owner:self options:nil] objectAtIndex:0];
[myScrollView setScrollEnabled:YES];
[myScrollView setContentSize:CGSizeMake(320, 600)];
[myScrollView setShowsVerticalScrollIndicator:YES];
[myScrollView setShowsHorizontalScrollIndicator:YES];
[myScrollView setPagingEnabled:NO];
[myScrollView addSubview:myView];
[self.view addSubview:myScrollView];
}
The NewSearch class is a UIView. It's xib is 320 wide by 600 high.
Upvotes: 2
Views: 3265
Reputation: 912
I'm not sure if I understood your question exactly, but it may or may not be similar to something I did.
See if adding this will work:
-(void)viewWillAppear:(BOOL)animated {
[self.myScrollView setFrame:CGRectMake(0, 0, 320, 600)];
}
Sorry if this isn't what you're looking for... I too am having issues with scrollview stuff. Good luck!
Upvotes: 1
Reputation: 1
I encountered a similar problem when I started using ios8. It turns out that touches were being trapped by something in the chain of view controllers. When I started using presentView Controller for the view controller containing the scrollview, then touches (and scrolling)started working again.
Upvotes: 0
Reputation: 58
This happens because you have same content size height as a frame height. Try to use same frame size as your main view on your screen and content size of the scroll view should be bigger then height (in your case) or width (this depends on which scroll you need horizontally or vertically).
For example
CGRect scrollFrame = CGRectMake(0, 0, [self.view bounds].size.height, self.view bounds].size.width);
UIScrollView *myScrollView = [[UIScrollView alloc]initWithFrame: scrollFrame];
...
[myScrollView setContentSize:CGSizeMake(320, **800**)];
As you see there is height of content size bigger then frame height of the your scroll view. So it can scroll horizontally. Also see the UIScrollView documentation.
Upvotes: 1