UITableview not placed correctly in uiscrollview - iphone

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.

Related

Gap between subviews of UIScrollView

I have two subviews of UIScrollView. One is textView and other is tableView. I made textview's height flexible based on its content, but I have one problem. If the text in the textview (which is parsed data) is small, there is a big gap between textview and tableview. Or if the text is too large then it covers the tableview. How can I keep the same gap between textview and tableview irrespective of the amount of content of text view? This is all done in IB.
Thanks.
you must handle both textview and tableview position dynamically like ur text view height and y-axis is
y-axis 10; height 40;
now in tableview setframe:cgrectmake(your X coordinate, textview.frame.size.height+20)
so its always show same difference between textview and tableview if your textview height is 60 than tableview y point is 60+20
- (void)loadView
{
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
pagingScrollView.pagingEnabled = YES;
pagingScrollView.backgroundColor = [UIColor blackColor];
pagingScrollView.showsVerticalScrollIndicator = NO;
pagingScrollView.showsHorizontalScrollIndicator = NO;
pagingScrollView.contentSize = CGSizeMake(pagingScrollViewFrame.size.width * [self imageCount],
pagingScrollViewFrame.size.height);
pagingScrollView.delegate = self;
self.view = pagingScrollView;
}
- (CGRect)frameForPagingScrollView {
CGRect frame = [[UIScreen mainScreen] bounds];
frame.origin.x -= PADDING;
frame.size.width += (2 * PADDING);
return frame;
}
- (CGRect)frameForPageAtIndex:(NSUInteger)index {
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
CGRect pageFrame = pagingScrollViewFrame;
pageFrame.size.width -= (2 * PADDING);
pageFrame.origin.x = (pagingScrollViewFrame.size.width * index) + PADDING;
return pageFrame;
}

How to modify the position of UIImageViews subviews inside a UIScrollView?

I'm trying to create gaps between the UIImageViews, which are subviews added into a UIScrollView.
And I thought I could do the following by modifying the CGRect of the UIImageView.
CGRect frame = imageView.frame;
frame.origin.x = <some values>
frame.origin.y = 0
imageView.frame = frame;
By modifying the x values, I assumed it will help me with the position of the UIImageViews inside the UIScrollView.
However, I realized whichever values I set for the frame.origin.x does not really matter. As each UIImageView inside the UIScrollView will be positioned side by side without any empty space.
I think you're doing it in the wrong way. You can't edit view's position by
view.frame.origin.x = something;
You should assign a new frame, like this:
view.frame = CGRectMake(x, y, w, h);
In your case it will look like this:
CGRect frame = imageView.frame;
CGFloat frameX = frame.origin.x;
CGFloat frameY = frame.origin.y;
CGFloat frameW = frame.size.width;
CGFloat frameH = frame.size.height;
frameX = <some values>
frameY = 0;
imageView.frame = CGRectMake(frameX, frameY, frameW, frameH);

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.

UIScrollView paging load from nib just 1 NIB show

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;

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;
}