Weird thing happening when adding UIViews to a ScrollView - iphone

I have a problem when adding UIViews to my scrollview:
NSArray *views = [NSArray arrayWithObjects:view1, view2, view3,nil];
for (int i = 0; i < views.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView addSubview:[views objectAtIndex:i]];
}
self.pageControl.numberOfPages = views.count;
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * views.count, self.scrollView.frame.size.height);
That's the code I'm using. The problem is that the ScrollView has the three pages, and all, but just the first page actually contains a view, which is the last one in the array. Why is this happening? I also tried:
for (UIView *view in views) {
[self.scrollView addSubview:view];
}
With the same result. What I'm doing wrong?

You need to set the position of the views you add:
[views objectAtIndex:i].frame = frame;
You are currently not changing the view's position anywhere, so all of them are probably at (0|0).

You're initializing frame in your loop but omit to do anything with it. You need to replace each view's frame to use it.

Related

jump to pages in a scrollview that is using UIPagecontrol

I have UIScrollview that contains different UITableviews. You can scroll through these UITableviews using a UIPagecontrol.
This is how I add my UITableview to the UIScrollview
- (void)loadPage:(NSInteger)page {
if (page < 0 || page >= self.pageStages.count) {
// If it's outside the range of what we have to display, then do nothing
return;
}
// Load an individual page, first seeing if we've already loaded it
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView == [NSNull null]) {
CGRect frame = self.scrollView.bounds;
//frame.size.width = 280.0f;
frame.size.height = self.scrollView.bounds.size.height;
float offset=20;
frame.size.width = frame.size.width-(2*offset);
frame.origin.x = (326 * page)+offset;
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.contentMode = UIViewContentModeScaleAspectFit;
tableStage.delegate = self;
tableStage.dataSource = self;
tableStage.tag = page;
tableStage.contentMode = UIViewContentModeScaleAspectFit;
[self.scrollView addSubview:tableStage];
[self.pageViews replaceObjectAtIndex:page withObject:tableStage];
}
}
Now I want that user can jump to a certain UITableview from another UIViewController. Therefore I have a variable pageNumber. When I set the pageNumber I call a method jumpToPage3 and I give the pageNumber with it. This is how the function looks like.
-(void)jumpToPage3:(NSNumber *)page{
NSLog(#"jump to page 3");
float offset=30;
float height = self.scrollView.bounds.size.height;
float width = 320 - (2*offset);
int pagee = [page intValue] - 1;
float x = (320 * pagee) + offset;
[self.scrollView scrollRectToVisible:CGRectMake(x, 0, width , height) animated:NO];
}
The problem is that it is not jumping to the correct page. It stays on the first page.
Can someone help me with this?
When you add content to the scroll view you also have to tell increase the contentSize

Adding a paged ui scroll view on top of another ui scroll view

I'm have a vertical scroll and I want to add a paged scroll view on top of it to go through a set of pictures. I have both of scroll views set up through the interface builder.
For the vertical scroll I have the following where scroll is the UIScrollView
-(void)createScroll{
[scroll setScrollEnabled:YES];
[scroll setContentSize:CGSizeMake(320, 1325)];
}
For the paged scroll I have the following where scrollView is the UIScrollView
for (int i = 0; i < array.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [array objectAtIndex:i];
[self.scrollView addSubview:subview];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * array.count, self.scrollView.frame.size.height);
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = array.count;
The problem is that the images are not showing up in the paged scroll view. I put the paged scroll into a single view test app and it works fine, however it doesn't work when I add it on top of the vertical scroll. I have a feeling that it has something to do with adding it to the subview, but I'm new to obj-c and not sure what order the subviews should go in. Anybody have any advice? Cheers!
I was creating the scroll view in the viewDidLoadMethod, once I created it in the (void)viewDidAppear:(BOOL)animated method the scroll views worked perfectly.

How to create a paging scrollView with space between views

I followed the tutorial about how to create a scrollView Page Control: http://www.iosdevnotes.com/2011/03/uiscrollview-paging/
This tutorial is really good and I implement well the code. Here my question:
I want to put a space between the my PageViews, but when it change the page it show the space between the views in the next page. The scroll must stop after the space when I change the page.
I changed the tutorial code here:
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
#define space 20
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = (self.scrollView.frame.size.width + space) * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subview];
[subview release];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count+ space*(colors.count-1), self.scrollView.frame.size.height);
}
It sounds like you want a “gutter” between the pages, so that each page fills the scroll view and the gutter is only visible while the user is dragging the view. The built-in Photos app does this, for example.
Make your scroll view wider by space points. For example, if you want the scroll view to appear to be as wide as the screen (320 points), with a 20 point margin between items, then make the scroll view 340 points wide, with the extra 20 points hanging off the right edge of the screen.
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
#define kGutterWidth 20
UIScrollView *scrollView = self.scrollView;
CGRect scrollViewFrame = scrollView.frame;
scrollViewFrame.size.width += kGutterWidth;
scrollView.frame = scrollViewFrame;
CGSize scrollViewSize = scrollView.bounds.size;
for (int i = 0; i < colors.count; i++) {
CGRect frame = CGRectMake(scrollViewSize.width * i, 0,
scrollViewSize.width - kGutterWidth, scrollViewSize.height);
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[scrollView addSubview:subview];
[subview release];
}
scrollView.contentSize = CGSizeMake(
colors.count * scrollViewSize.width,
scrollViewSize.height);
}
I've been down this road before, and I would advise laying out the scroll view as the tutorial code recommends, with no space between the subviews.
Instead, give each subview another subview whose frame is inset from it's parent...
CGRect scrollViewFrame = self.scrollView.frame;
for (int i=0; i<colors.count; i++) {
CGRect frame = CGRectMake(scrollViewFrame.size.width * i, 0, scrollViewFrame.size.width, scrollViewFrame.size.height);
// this will be our framing view, full size with no gaps
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [UIColor whiteColor];
// here's the trick - use CGRectInset on the framing view's bounds
UIView *colorView = [[UIView alloc] initWithFrame:CGRectInset(subview.bounds, 10, 10)];
colorView.backgroundColor = [colors objectAtIndex:i];
[subview addSubview:colorView];
[self.scrollView addSubview:subview];
// aren't you using ARC? your tutorial is probably older than ARC
// would strongly suggest converting to ARC
[subview release];
[colorView release];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
The benefit of this approach is that the math for the top level subview layout remains a simple multiple of the width. The only place you'll refer to that inset constant is on the CGRectInset where it belongs.

Scroll view issue

friends
I develop one scroll view set its content size and set three view on scroll view and its scroll fine and i also want it move when segmentcontrol tap but it was not happen my view is not move on scroll can some one help me to figure out this problem.
hope i understand your question
try this
-(IBAction)SegmentControll:(id)sender
{
[myTableView removeFromSuperview];
if (SegmentControll.selectedSegmentIndex==0)
{
Yourscollview.scrollEnabled=NO;
}
if (SegmentControll.selectedSegmentIndex==1)
{
Yourscollview.scrollEnabled=YES;
}
}
if need more help ask me.
Move your scroll-view with tap on the Segment-control...Here, I am providing you sample code...you just need to decide on which segment index you want to move scroll view...
if (SegmentControll.selectedSegmentIndex==0)
{
if ( self.scrollView.contentOffset.x <= self.scrollView.frame.size.width ) {
CGRect frame;
frame.origin.x = self.scrollView.contentOffset.x + self.scrollView.frame.size.width;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
//pageControlBeingUsed = YES;
}
}
else if (SegmentControll.selectedSegmentIndex==1)
{
if ( self.scrollView.contentOffset.x >= self.scrollView.frame.size.width ) {
CGRect frame;
frame.origin.x = self.scrollView.contentOffset.x - self.scrollView.frame.size.width;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
//pageControlBeingUsed = YES;
}
}
I hope this will help you...

iPhone:UIScrollView with Paging in Landscape Mode Issue

I am build an app in Landscape mode only and I have an issue is that not fill up color with width in a Landscape mode in iPhone but I can't understand how can i do that so please give me some idea to develop this functionality.
Refer https://github.com/cwalcott/UIScrollView-Paging
Thanks in advance.
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
pageControlBeingUsed = NO;
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < colors.count; i++)
{
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subview];
[subview release];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = colors.count;
[super viewDidLoad];
}
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
if (!pageControlBeingUsed)
{
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
pageControlBeingUsed = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
pageControlBeingUsed = NO;
}
- (IBAction)changePage
{
// Update the scroll view to the appropriate page
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
// Keep track of when scrolls happen in response to the page control
// value changing. If we don't do this, a noticeable "flashing" occurs
// as the the scroll delegate will temporarily switch back the page
// number.
pageControlBeingUsed = YES;
}
Solve my issue by just remove inner subView in Autosizing property of UIScrollView in interface Builder.
I think the reason will be
self.scrollView.frame.size returns (768,1024) in viewDidLoad: method but actually you need (1024,768). To solve this,
1) In IB, Attribute Inspecter change orientation to landscape mode.
or
2) Change
frame.size = self.scrollView.frame.size;
to
frame.size = CGSizeMake(1024,768);