iPhone SDK: UIScrollView does not scroll - iphone

I just can't get my scroll view to actually scroll.. Everything displays fine except it just won't scroll..... Here's my code:
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 600)];
scrollView.contentSize = CGSizeMake(320, 600);
scrollView.scrollEnabled = YES;
scrollView.clipsToBounds = YES;
[self.view addSubview:scrollView];
planView = [[WorkoutPlanView alloc] initWithImage:[UIImage imageNamed:#"WorkoutTable.png"]];
planView2 = [[WorkoutPlanView alloc] initWithImage:[UIImage imageNamed:#"WorkoutTable.png"]];
planView2.frame = CGRectMake(0, 164, 320, 165);
planView3 = [[WorkoutPlanView alloc] initWithImage:[UIImage imageNamed:#"WorkoutTable.png"]];
planView3.frame = CGRectMake(0, 328, 320, 165);
planView4 = [[WorkoutPlanView alloc] initWithImage:[UIImage imageNamed:#"WorkoutTable.png"]];
planView4.frame = CGRectMake(0, 505, 320, 165);
[scrollView addSubview:planView];
[scrollView addSubview:planView2];
[scrollView addSubview:planView3];
[scrollView addSubview:planView4];
}
How do I fix this problem?

The contentSize must be larger than the scrollview's frame for there to be anything to scroll. :)
In this case, they're both (320, 600) so change your scrollView's init to
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

your scrollview frame should be smaller than contentSize,so only you can make scrollview scrollable.

Related

Add 10 different UIImages in UIScrollView

I am trying to add various UIImages under UIImageView and allow them to scroll with UIScrollView. I am not sure how to add various images under UIImageView and let them scroll.
Below is my code which adds an image on UIImageView and make it scrollable.
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:#"ae.jpg"];
imageView = [[UIImageView alloc]initWithImage:image];
imageView.frame = [[UIScreen mainScreen] bounds];
imageView.contentMode = (UIViewContentModeScaleAspectFit);
imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
imageView.backgroundColor = [UIColor clearColor];
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
scrollView.contentMode = (UIViewContentModeScaleAspectFit);
scrollView.contentSize = CGSizeMake(image.size.width,960);
scrollView.pagingEnabled = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.alwaysBounceVertical = NO;
scrollView.alwaysBounceHorizontal = NO;
scrollView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
scrollView.maximumZoomScale = 2.5;
scrollView.minimumZoomScale = 1;
scrollView.clipsToBounds = YES;
[scrollView addSubview:imageView];
[image release];
[imageView release];
[self.view addSubview:scrollView];
}
The idea is basically simple. Let's assume you want to place 3 images in UIScrollView.
Each of images is 300x300. In this case you'll have scroll view with frame:
scrollView.contentSize = CGSizeMake(image.size.width,900);
For every image you must have it's UIImageView with proper frame:
imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, 300, 300)];
imgView2 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 300, 300, 300)];
imgView3 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 600, 300, 300)];
imgView1.image = [UIImage imageNamed:#"ProperName.png"];
...
(pay attention to the yOrigin (2nd value in CGRectMake))
and then as you did:
[scrollView addSubview:imgView1];
[scrollView addSubview:imgView2];
[scrollView addSubview:imgView3];
[imgView1 release];
[imgView2 release];
[imgView3 release];
Of course, it's a brief code, you'll optimize it ;)

uiscrollview not scrolling horizontally

My UIScrollView below is not scrolling horizontally, Please help me here..
FirstView = [[UIView alloc] initWithFrame:CGRectMake(60, 0, 100, 150)];
[FirstView setBackgroundColor:[UIColor clearColor]];
SecondView = [[UIView alloc] initWithFrame:CGRectMake(320+60, 0, 100, 150)];
[SecondView setBackgroundColor:[UIColor clearColor]];
HolderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1000, 150)];
scrHorizontalScroll = [[UIScrollView alloc]initWithFrame:HolderView.frame];
[scrHorizontalScroll setBackgroundColor:[UIColor redColor]];
[scrHorizontalScroll setContentSize:CGSizeMake(999, 150)];
[scrHorizontalScroll setScrollEnabled:YES];
[HolderView addSubview:scrHorizontalScroll];
[scrHorizontalScroll addSubView:FirstView]; [scrHorizontalScroll addSubView:SecondView]; [HolderView addSubView: scrHorizontalScroll];
The HolderView is not scrolling horizontally but which happen, please help
Note: I have a mainView which adds this HolderView in its top section as per the frame coordinates.
[mainView addSubView:label].... [mainView addSubView:HolderView];
This is complete structure
A scroll view will only scroll when its content size is bigger than its frame.
FirstView = [[UIView alloc] initWithFrame:CGRectMake(60, 0, 100, 150)];
[FirstView setBackgroundColor:[UIColor clearColor]];
SecondView = [[UIView alloc] initWithFrame:CGRectMake(320+60, 0, 100, 150)];
[SecondView setBackgroundColor:[UIColor clearColor]];
HolderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 150)];
scrHorizontalScroll = [[UIScrollView alloc]initWithFrame:HolderView.frame];
[scrHorizontalScroll setBackgroundColor:[UIColor redColor]];
[scrHorizontalScroll setContentSize:CGSizeMake(999, 150)];
[scrHorizontalScroll setScrollEnabled:YES];
[HolderView addSubview:scrHorizontalScroll];
[scrHorizontalScroll addSubView:FirstView]; [scrHorizontalScroll addSubView:SecondView]; [HolderView addSubView: scrHorizontalScroll];
//set with of the scrHorizontalScroll as second view,increase content view size to x axis.

UIScrollView: Scrolling doesn't work

I'd like to scroll my content in a UIScrollView. But I think I a made a mistake.
// setup view
CGRect appFrame = [UIScreen mainScreen].applicationFrame;
CGRect frame = CGRectMake(0, 0, appFrame.size.width, appFrame.size.height);
self.view = [[[UIView alloc] initWithFrame:frame] autorelease];
// setup wrapper
UIScrollView *wrapper = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, appFrame.size.width, appFrame.size.height + 320)];
wrapper.backgroundColor = [UIColor redColor];
wrapper.scrollEnabled = YES;
[self.view addSubview:wrapper];
// title (simple version here)
title.text = "Hello World";
[wrapper addSubview:title];
Instead of setting large frame you must set UIScrollView's content size:
UIScrollView *wrapper = [[UIScrollView alloc] initWithFrame:
CGRectMake(0, 0, appFrame.size.width, appFrame.size.height)];
wrapper.contentSize =CGSizeMake(appFrame.size.width, appFrame.size.height + 320);

How do I put viewController's into a UIScrollView

I want to initialize 5 viewController's that I want to be able to flick between in a UIScrollView, when my app loads.
Here is an example of how you can do this:
- (void)viewDidLoad
{
//standard UIScrollView is added
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[self.view addSubview:scrollView];
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(320*2, 460); //this must be the appropriate size!
//required to keep your view controllers around
controllers = [[NSMutableArray alloc] initWithCapacity:0];
//just adding two controllers
LabeledViewController *one = [[LabeledViewController alloc] initWithPosition:0 text:#"one"];
[scrollView addSubview:one.view];
[controllers addObject:one];
LabeledViewController *two = [[LabeledViewController alloc] initWithPosition:1 text:#"two"];
[scrollView addSubview:two.view];
[controllers addObject:two];
}
LabeledViewController is pretty simple, but you can add as much to it as you want:
#implementation LabeledViewController
- (id)initWithPosition:(NSInteger)position text:(NSString*)text
{
if (self = [super init]) {
myPosition = position;
myText = [text retain];
}
return self;
}
- (void)viewDidLoad
{
//this will setup the position in the UIScrollView
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(320*myPosition, 0, 320, 460)];
self.view = view;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 320, 50)];
label.text = myText;
[self.view addSubview:label];
}

Adding a UIButton to UIScrollView

I am trying to add the UIButton to UIScrollView.
I have added the UIImageView with background image *(size: 320 * 620)*.
Then I added this UIImageView to UIScrollView, and It works fine.
But now I want to add UIButton at position at : (60, 500); (below screen that would appear after scrolling).
I have tried following code, but the button is added on UIView not on scrollview. Button is not getting displayed on the top.
Code :
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.hidden = TRUE;
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"SingleModelVar.png"]];
self.imageView = tempImageView;
[tempImageView release];
imageView.userInteractionEnabled = YES;
scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; //fit to screen
//scrollView.delaysContentTouches = NO;
scrollView.contentSize = CGSizeMake(320, imageView.frame.size.height); //imageView.frame.size.height=620 here
scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = 0.75;
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
//The Problem begins ..........
btnStartDate=[[UIButton alloc] initWithFrame:CGRectMake(60,500,100,20)];
[scrollView addSubview:btnStartDate];
//[self.view addSubview:btnStartDate];
btnEndDate=[[UIButton alloc] initWithFrame:CGRectMake(60,530,100,20)];
[scrollView addSubview:btnEndDate];
//[self.view addSubview:btnEndDate];
[scrollView addSubview:imageView];
[[self view] addSubview:scrollView];
}
it is not displayed at the top because you added them before you added the imageView.
Put [scrollView addSubview:imageView]; before btnStartDate=[[UIButton alloc] initWithFrame:CGRectMake(60,500,100,20)];