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);
Related
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 ;)
Here is my code:
mainView:
NSArray *btArray = [NSArray arrayWithObjects:#"1",#"1",#"1",#"1",#"1",nil];
ButtonBarComponent *bottomeButtonBar = [[ButtonBarComponent alloc] initButtonBarComponentWithButtonArray:btArray];
bottomeButtonBar.frame = CGRectMake(0, 340, 320, 200);
[self.view addSubview:bottomeButtonBar];
subView:
-(id)initButtonBarComponentWithButtonArray:(NSArray *)btArray {
self = [[UIView alloc] initWithFrame:CGRectMake(0, 340, 320, 200)];
self.backgroundColor = [UIColor whiteColor];
}
but why I cant run the code in touches event?
try setting user interaction enabled for your view and subview
I just want to scroll only one page then to another.
I set m_pScrollView.pagingEnabled = YES and m_pScrollView.bounces = NO but when I scroll to the edge of the page it goes out of the page range and then back, likes an inertia effect.
I just want it scoll only one page not more. Can anyone help me?
- (void)viewDidLoad {
[super viewDidLoad];
m_pScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 768, 1024)];
m_pScrollView.delegate = self;
m_pScrollView.contentSize = CGSizeMake(768 * 3, 1024);
m_pScrollView.pagingEnabled = YES;
m_pScrollView.bounces = NO;
m_pScrollView.alwaysBounceHorizontal = NO;
m_pScrollView.alwaysBounceVertical = NO;
Myview *myview = [[Myview alloc]initWithFrame:CGRectMake(0, 0, 768, 1024)];
myview.backgroundColor = [UIColor redColor];
Myview *myview2 = [[Myview alloc]initWithFrame:CGRectMake(768, 0, 768, 1024)];
myview2.backgroundColor = [UIColor blueColor];
Myview *myview3 = [[Myview alloc]initWithFrame:CGRectMake(768 * 2, 0, 768, 1024)];
myview3.backgroundColor = [UIColor grayColor];
[m_pScrollView addSubview:myview];
[m_pScrollView addSubview:myview2];
[m_pScrollView addSubview:myview3];
[self.view addSubview:m_pScrollView];
}
You have to set the content size like this
m_pScrollView.contentSize = CGSizeMake(x,y);
For more about this Create UIScrollview programmatically
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.
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];
}