I have a question how to add UIScrollView in UIView, so that the UIScrollView could work properly.
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, container.frame.size.width/2, container.frame.size.height/2)];
scroll.pagingEnabled = YES;
scroll.scrollEnabled = YES;
[scroll setBackgroundColor:[UIColor redColor]];
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++)
{
CGFloat xOrigin = i * container.frame.size.width/2;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, container.frame.size.width, container.frame.size.height)];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scroll addSubview:awesomeView];
[awesomeView release];
}
scroll.contentSize = CGSizeMake(container.frame.size.width/2 * numberOfViews, container.frame.size.height);
[container addSubview:scroll];
Above code is from tutorial: http://idevzilla.com/2010/09/16/uiscrollview-a-really-simple-tutorial/ But it doesn't work for me.
EDIT:
If you have a problem that you have set up a scrollview properly but it's not working, make sure that you are not overlaying with another UIView your scrollview. That was my problem. Solved!
Add a UIStackView to the UIScrollView. Set the constraints: Leading , Trailing , Top & Bottom should be equal to the ones from UIScrollView. Set up an equal Width constraint between the UIStackView and UIScrollView . Set Axis = Vertical, Alignment = Fill, Distribution = Equal Spacing, and Spacing = 0 on the UIStackView.
You can use below code to add UIScrollView on yourView :-
Step 1:
Delegate "UIScrollViewDelegate" to your ViewController.h
for example:
@interface yourViewController:UIViewController<UIScrollViewDelegate>
Step 2:
//create you UIScrollView
UIScrollView *MyScrollVw= [[UIScrollView alloc]initWithFrame:CGRectMake(0 ,0 ,320 ,480)];
MyScrollVw.delegate= self;
[MyScrollVw setShowsHorizontalScrollIndicator:NO];
[MyScrollVw setShowsVerticalScrollIndicator:YES];
MyScrollVw.scrollEnabled= YES;
MyScrollVw.userInteractionEnabled= YES;
[yourView addSubview:MyScrollVw];
MyScrollVw.contentSize= CGSizeMake(320 ,1500);//(width,height)
Step 3:
you want to implement the scrollView Delegates in ViewController.m
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return imgView;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSLog(@"Did end decelerating");
//do your code here
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(@"Did scroll");
//do your code here
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
NSLog(@"Did end dragging");
//do your code here
}
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
NSLog(@"Did begin decelerating");
//do your code here
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"Did begin dragging");
//do your code here
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With