Want to make my UIScrollView be infinite (like dates in a calendar) - ios

I have a UIScrollView like so:
self.view.backgroundColor = [UIColor redColor];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scroll addSubview:awesomeView];
}
[scroll setShowsHorizontalScrollIndicator:NO];
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:scroll];
This is the right gestures I want and is fine for a finite set of custom subviews, but how do I make the scollview go infinite?
I want the subviews to be dates and swiping left and right will bring the next or previous date.

Either use UIScrollViewDelegate method -(void)scrollViewDidScroll:(UIScrollView*)scrollView and adjust your view positions and the scrollviews content offset there, or have a look at Apple's example on how to create a infinite horizontal ScrollView by subclassing the UIScrollView: https://developer.apple.com/library/ios/samplecode/StreetScroller/

Related

iOS 8 Custom keyboard UIScrollview isn't working

I have tried to add UISchollView in custom keyboard view but it shows view but not scrolling.
here is my code. i have also tried self.view but its not working either.
UIScrollView *scrollview1 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
NSInteger viewcount= 8;
for (int i = 0; i <viewcount; i++)
{
CGFloat y = i * 50;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, y,50, 50)];
view.backgroundColor = (i%2==0)?[UIColor greenColor]:[UIColor blueColor];
[scrollview1 addSubview:view];
}
scrollview1.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height *viewcount);
scrollview1.backgroundColor = [UIColor redColor];
scrollview1.pagingEnabled=YES;
[self.inputView addSubview:scrollview1];
UIScrollView will scroll if its contentSize is greater then its frame size. So try setting its contentSize property and also check if userInteraction is enabled or not.
Hope it will work.

Row height of the uitableview in differing gradually

I have two uitableviews in a view controller. First table is set on a uiscrollview programatically and the other table on self.view from storyboard. I have set the rowHeight for both the table as 45. But gradually the height of one table keeps increasing as the rows increase like the screenshot I have shown.
Code goes like this :
scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(130, 45, self.view.frame.size.width-130, self.view.frame.size.height-45)];
scrollview.backgroundColor = [UIColor colorWithRed:0.42 green:0.27 blue:0.60 alpha:1.0];
tableGradeValue = [[UITableView alloc] initWithFrame:CGRectMake(0, 35, self.view.frame.size.width, self.view.frame.size.height-75) style:UITableViewStylePlain];
tableGradeValue.delegate = self;
tableGradeValue.dataSource = self;
self.tableCourse.delegate = self;
self.tableCourse.dataSource = self;
tableGradeValue.rowHeight = 45;
scrollview.bounces = NO;
for(int i = 0; i< [res6Keys count]; i++)
{
CGFloat y = i * 150;
UILabel *lblCell = [[UILabel alloc] initWithFrame:CGRectMake(y, 1, 140, 31)];
lblCell.tag = i+1;
[scrollview addSubview:lblCell];
lblCell.backgroundColor = [UIColor clearColor];
lblCell.textColor = [UIColor colorWithRed:0.93 green:0.92 blue:0.96 alpha:1.0];
lblCell.text = [res6Keys objectAtIndex:i];
}
[scrollview addSubview:tableGradeValue];
scrollview.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:scrollview];
And the other tableview is set from storyboard normally and the rowHeight is 45.
I don't know wether this was the problem. But strangely I got the answer. I dint give #property and #synthesis to the table which was over the scrollview. I now gave property and synthesis and it got working.

How to create UIScrollview inside UIScrollview programmatically

Please tell me how to set scrollview inside a scrollview like nested process.
The following code works partially.
int x=10;
int y=10;
for(int i=0; i<5; i++)
{
UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(x, y, 50, 50)];
scrollview.showsVerticalScrollIndicator=YES;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
scrollview.backgroundColor = [UIColor greenColor];
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(50,50);
y=y+95;
}
Now all I can see are 3 scrollviews and the others are hidden. How can I create main scroll to view so that the child scrollViews are not hidden?
You need to have an initial scrollView that you then put these scrollViews in.
UIScrollView * mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
mainScrollView.contentSize = CGSizeMake(50, (y + 95) * 5);
// further configure
[self.view addSubview: mainScrollView];
Then change
[self.view addSubview:scrollview];
To
[mainScrollView addSubview: scrollView];
//I have created Two Scroll view programmatically this way
UIScrollView *scrollViewOuter = [[UIScrollView alloc] initWithFrame:CGRectMake(100.0f, 100.0f, 600.0f, 600.0f)];
scrollViewOuter.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
scrollViewOuter.contentSize = CGSizeMake(2000.0f, 2000.0f);
UIScrollView *scrollViewInner = [[UIScrollView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 200.0f, 200.0f)];
scrollViewInner.backgroundColor = [UIColor whiteColor];
scrollViewInner.contentSize = CGSizeMake(2000.0f, 2000.0f);
[scrollViewOuter addSubview:scrollViewInner];
[self.window addSubview:scrollViewOuter];
//You can change frame and use in your own way
Just create a parent scrollview that is big enough to hold the 5 smaller ones, then change this line:
[self.view addSubview:scrollview];
to
[parentScrollView addSubview:scrollview];

How can i add multiple images to my scrollView in ios

i have scroll View with three slides,i want to add images to my scroll view.this is my code after this what do i need to add
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor redColor];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scroll addSubview:awesomeView];
[awesomeView release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll release];
}
If you want to add Images horizontally (say 3 images), than you have to add scroll view with the width of 3*yourImageView.frame.size.width and than you can add those Image on (x=0,y=0),
(x = yourImageView.frame.size.width, y=0), and (x = 2*yourImageView.frame.size.width, y=0)
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, yourImageView.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 3;
imageView1.frame = CGrectMake(0,0,imageView1.frame.size.width,imageView1.frame.size.height);
imageView2.frame = CGrectMake(0,imageView1.frame.size.width,imageView2.frame.size.width,imageView2.frame.size.height)
imageView3.frame = CGrectMake(0,2*imageView1.frame.size.width,imageView3.frame.size.width,imageView3.frame.size.height)
[scroll addSubview:imageView1];
[scroll addSubview:imageView2];
[scroll addSubview:imageView3];
enable horizontal scrolling

Ipad tableview and imageview

How to insert four tableview in a scrollview by pagination.i know how to do this ,but i need a image view as first page and then tableview as second,third and forth page.
Thankz in advance
_scroll=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 55, 1000, 460)];
_scroll.backgroundColor=[UIColor clearColor];
_scroll.pagingEnabled=YES;
_scroll.contentSize = CGSizeMake(1000*5, 460);
[_scroll setBackgroundColor:[UIColor lightGrayColor]];
CGFloat x=0;
for(int i=1;i<6;i++)
{
UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(x+10, 10, 750, 440) style:UITableViewStylePlain];
[table setBackgroundColor:[UIColor whiteColor]];
table.rowHeight = 120;
table.separatorColor = [UIColor grayColor];
table.delegate=self;
//table.dataSource=self;
[_scroll addSubview:table];
[table release];
x+=1000;
}
[self.view addSubview:_scroll];
_scroll.showsHorizontalScrollIndicator=NO;
[_scroll release];
Ignoring the recommendation that UITableViews are not embedded inside scroll views as they are scrollviews in themself. So dont do this (not a joke).
You add the imageview with addSubview: just like the tableviews you want add. They are all UIViews and are all treated the same way.
So for a hypothetical scrollview with size (100,100) and imageview/tableviews of size (100,100)
imageview.frame = CGRectMake(0,0,100,100);
[scrollview addSubview:imageview];
tableview1.frame = CGRectMake(100,0,100,100);
[scrollview addSubview:tableview1];
tableview2.frame = CGRectMake(200,0,100,100);
[scrollview addSubview:tableview2];
tableview3.frame = CGRectMake(300,0,100,100);
[scrollview addSubview:tableview3];
scrollview.contentSize = CGSizeMake(400,100);
scrollview.pagingEnabled = YES;

Resources