UIScrollView paging load from nib just 1 NIB show - iphone

i'm new on object-C, i want show some NIB file to UIScrollView with paging, i'm do it but just 1 NIB showing, other nib not show, the sample i make 2 page on each page have NIB, this is code :
bbottompageused = NO;
CGRect frame;
int tview=2;
mycontact = [[MyContact alloc] initWithNibName:#"MyContact" bundle:nil];
myphoto = [[MyPhoto alloc] initWithNibName:#"MyPhoto" bundle:nil];
frame.origin.x = self.midleScroll.frame.size.width * 1;
frame.origin.y = 0;
frame.size = self.midleScroll.frame.size;
[self.midleScroll addSubview:mycontact.view];
frame.origin.x = self.midleScroll.frame.size.width * 2;
frame.origin.y = 0;
frame.size = self.midleScroll.frame.size;
[self.midleScroll addSubview:myphoto.view];
self.midleScroll.contentSize = CGSizeMake(self.midleScroll.frame.size.width * tview, self.midleScroll.frame.size.height);
self.midlePage.currentPage = 0;
self.midlePage.numberOfPages = tview;
Have anyone suggestion for solve this ?
Thanks all,

assign the frame that you have to your views (myContact, myPhoto) and then add them to the scroll view.

You configure the frame but you don't assign this frame to myContact.view and myPhoto.view.
Hence, both of the myContact.view.frame and myPhoto.view.frame remain unchanged.
Do the following:
bbottompageused = NO;
CGRect frame;
int tview=2;
mycontact = [[MyContact alloc] initWithNibName:#"MyContact" bundle:nil];
myphoto = [[MyPhoto alloc] initWithNibName:#"MyPhoto" bundle:nil];
frame.origin.x = self.midleScroll.frame.size.width * 1;
frame.origin.y = 0;
frame.size = self.midleScroll.frame.size;
// Assign the frame
myContact.view.frame = frame;
[self.midleScroll addSubview:mycontact.view];
frame.origin.x = self.midleScroll.frame.size.width * 2;
frame.origin.y = 0;
frame.size = self.midleScroll.frame.size;
// Assign the frame
myPhoto.view.frame = frame;
[self.midleScroll addSubview:myphoto.view];
self.midleScroll.contentSize = CGSizeMake(self.midleScroll.frame.size.width * tview, self.midleScroll.frame.size.height);
self.midlePage.currentPage = 0;
self.midlePage.numberOfPages = tview;

Related

UITableview not placed correctly in uiscrollview

I have a UIscrollView that uses a pagecontrol. I'm adding tables to the scrollview in code. The tableviews should have a padding of 20px on each side. So I do the following.
CGRect frame = self.scrollView.bounds;
frame.size.width = 280.0f;
frame.size.height = self.scrollView.bounds.size.height;
frame.origin.x = (320 * page)+20;
NSLog(#"orgin x is %d",(320 * page)+20);
NSLog(#"orgin x is %f",frame.origin.x);
frame.origin.y = 0;
UITableView *tableStage = [[UITableView alloc]initWithFrame:frame];
tableStage.backgroundColor = [UIColor colorWithRed:(250/255.0) green:(248/255.0) blue:(247/255.0) alpha:100];
tableStage.delegate = self;
tableStage.dataSource = self;
tableStage.tag = page;
tableStage.contentMode = UIViewContentModeScaleAspectFit;
[self.scrollView addSubview:tableStage];
[self.pageViews replaceObjectAtIndex:page withObject:tableStage];
But I'm getting the following result.
Like you can see the next tableview is always moving something more to the left. Any idea how I can fix this?
Try this
CGRect frame = self.scrollView.bounds;
float offset=20;
frame.size.width = frame.size.width-(2*offset);
frame.origin.x = (320 * page)+offset;
frame.origin.y = 0;
UITableView *tableStage = [[UITableView alloc]initWithFrame:frame];
and no need for this line
tableStage.contentMode = UIViewContentModeScaleAspectFit;
CGRect frame = self.scrollView.bounds;
frame.size.width = 280.0f;
frame.size.height = self.scrollView.bounds.size.height;
frame.origin.x = (self.scrollView.bounds.size.width * page)+(self.scrollView.bounds.size.width - (frame.size.width/2));
frame.origin.y = 0;
If you set the content size properly and the location of each new table view it will work perfectly.Also enable the paging.

how to add subviews in a 'for' loop

I'm simply trying to add a UIView for every object in an array without displaying more than 3 on screen but the views aren't next to each other. There is a big gap (one view width) between each view. Here's what I've got;
int numberOfUsersOnScreen;
if (array.count < 3) {
numberOfViewsOnScreen = array.count;
}else{
numberOfUsersOnScreen = 3;
}
double width = (self.scrollView.frame.size.width/numberOfViewsOnScreen);
CGRect r = CGRectMake(0, 0, width, 1200);
[self.usersScrollView setContentSize:CGSizeMake(width*array.count, 0)];
for (int i = 0; i < users.count; i++) {
r.origin.x = width * i;
UIView * view = [[UIView alloc] initFrame:r];
[self.scrollView addSubview:view];
}
Try this:
int xPosition = 0;
for (int i = 0; i < users.count; i++) {
UIView * view = [[UIView alloc] initFrame:CGRectMake(xPosition, 0, width, 1200)];
[self.scrollView addSubview:view];
xPosition += width;
}
Instead of
[self.scrollView setContentSize:CGSizeMake(width*array.count, 0)];
It should be
[self.scrollView setContentSize:CGSizeMake(width*array.count, self.scrollView.frame.size.height)];
for (int i = 0; i < users.count; i++) {
r.origin.x = width * i;
UIView * view = [[UIView alloc] initFrame:r];
[self.scrollView addSubview:view];
}
In this way you have also a memory leak on view(s) object
for (int i = 0; i < users.count; i++) {
r.origin.x = width * i;
UIView * view = [[UIView alloc] initFrame:r];
[self.scrollView addSubview:view];
[view release];
}

ios : How to load images from URL to page control using scroll view

I'm new in iOS. Now I want to load view images from URL to page control using scroll view. When I run the program the first image is shown but when I try to slide to next image or I move to another page program is not responding. My guess is probably related to the size of image.
The following is my code to load the image. I put this code in -(void) viewDidLOad...
CGRect screenShot = screenView.frame;
screenShot.origin.y = description.frame.origin.y + description.frame.size.height + 30;
screenView.frame = screenShot;
pageControlBeingUsed = NO;
for (int i = 0; i < [myArrayImagesURL count]; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIImageView *iv = [[[UIImageView alloc] initWithFrame:CGRectMake(scrollView.bounds.size.width * i, 0, scrollView.bounds.size.width, scrollView.bounds.size.height)] autorelease];
iv.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:[myArrayImagesURL objectAtIndex:i]]]];
[self.scrollView addSubview:iv];
[iv release];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * [myArrayImagesURL count], self.scrollView.frame.size.height);
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = [myArrayImagesURL count];
It looks like you have set the imageView to autorelease and the released it. This should be wrong. Try removing the autorelease tag when you create the imageView. It should be properly retained now.
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(scrollView.bounds.size.width * i, 0, scrollView.bounds.size.width, scrollView.bounds.size.height)];

Resize UIScrollView

I am creating an app like the "PageControl" from apple.
An scrollview with another scrollview inside for each page.
UIScrollView * scrollForPage = [[UIScrollView alloc]init];
scrollForPage.pagingEnabled = NO;
scrollForPage.contentSize = CGSizeMake(mainScroll.frame.size.width, 200 + mainScroll.frame.size.height+150);
scrollForPage.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
scrollForPage.clipsToBounds = YES;
CGRect frame = mainScroll.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
CGRect frame2 = frame;
frame2.origin.x = 0;
frame2.origin.y = 0;
controller.view.frame = frame2;
scrollForPage.frame = frame;
[scrollForPage addSubview:controller.view];
[mainScroll addSubview:scrollForPage];
[scrollForPage release];
however my controllerĀ“s view have an dynamic size, how can I resize the "scrollForPage" to have the same size??
Thanks
The code to detect the size of a view is this:
CGRect screenSize = [[UIScreen mainScreen] bounds];
Just make the frame of the scroll view equal to screenSize, and call the above line often.

UIAlertview Button columns

In iphone application can we set number of columns in UIAlertView as if I have six buttons in UIAlertView then how can I show it in 2 columns and 3 rows. If any one done it kindly share it how it will done ..
Any sample code will be extra help
A UIAlertView is just a UIView. So you can manually add the buttons to the UIAlertView in a two column configuration. There is an example here that demonstrates adding UITextFields, but I'm sure you can adapt it.
Note that having two many buttons in the UIAlertView might get Apple's back up ;-)
I have done it like this
NSArray *subViewArray = Topicdialog.subviews;
float y = 60.0f;
for(int x=2;x<[subViewArray count];x += 2){
UIView *button = [subViewArray objectAtIndex:x];
CGRect frame = button.frame;
frame.size.width = 120.0f;
frame.size.height = 42.0f;
frame.origin.x = 20.0f;
frame.origin.y = y;
button.frame = frame;
UIView *button1 = [subViewArray objectAtIndex:x + 1];
frame = button1.frame;
frame.size.width = 120.0f;
frame.size.height = 42.0f;
frame.origin.x = 152.0f;
frame.origin.y = y;
button1.frame = frame;
y = y + 48.0f;
}