How to create UIScrollview inside UIScrollview programmatically - ios

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

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.

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

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/

Arrange UIscrollView to back

Maybe a huge noob question but i cant seem to figure it out, even after some time googling i cant find out to arrange a scrollview underneeth a navigation bar.
My code:
- (void)loadView {
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
scroll.backgroundColor = [UIColor blackColor];
scroll.delegate = self;
image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Plattegrond DeFabrique schematisch.png"]];
scroll.contentSize = image.frame.size;
[scroll addSubview:image];
scroll.minimumZoomScale = scroll.frame.size.width / image.frame.size.width;
scroll.maximumZoomScale = 2.0;
[scroll setZoomScale:scroll.minimumZoomScale];
self.view = scroll;
}
Try inserting the view behind the superview or try sendSubviewToBack.
[[self.view superview] insertSubview:scroll belowSubview:self.view];
or
[self.view sendSubviewToBack:scroll];
It sounds like you are just wanting to add the scroll view to the self.view though. Do as the other answer suggest and create the scrollView from the self.view frame then add it to your self.view.
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:self.view.frame]; // Thilina
[self.view addSubview:scroll];
Since you are passing the frame of [UIScreen mainScreen] at the time of allocating the UIScrollView , the size of UIScrollView is overlapping the UINavigation . Replace it with
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:self.view.frame];
also rather than replacing the self.view with the scrollView , try to add the scrollView as subview on self.view
[self.view addSubview:scrollView];
Do the following task in the viewDidLoad
Hope it will help you.
You can simply use the self.view's frame when creating the ScrollView. This will solve your problem.
- (void)loadView {
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:self.view.frame];
scroll.backgroundColor = [UIColor blackColor];
scroll.delegate = self;
image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Plattegrond DeFabrique schematisch.png"]];
scroll.contentSize = image.frame.size;
[scroll addSubview:image];
scroll.minimumZoomScale = scroll.frame.size.width / image.frame.size.width;
scroll.maximumZoomScale = 2.0;
[scroll setZoomScale:scroll.minimumZoomScale];
[self.view addSubView:scroll]; //MarkM
}

Cannot scroll when adding UIScrollView to UIView

I'm trying to add a scrollview to my UIView, and put a label, textview, image on it but the scroll bar doesn't show up and I can't scroll.
//faux view
UIView* fauxView = [[[UIView alloc] initWithFrame: CGRectMake(10, 10, 200, 200)]autorelease] ;
[self.bgView addSubview: fauxView];
//the new panel
self.bigPanelView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bgView.frame.size.width, self.bgView.frame.size.height)]autorelease];
self.bigPanelView.center = CGPointMake( self.bgView.frame.size.width/2, self.bgView.frame.size.height/2);
self.bigPanelView.backgroundColor = self.initialBackgroundColor;
UIScrollView * scroll = [[UIScrollView alloc] initWithFrame:self.bigPanelView.bounds];
[scroll setContentSize:CGSizeMake(200, 200)];
[self.bigPanelView addSubview:scroll];
scroll.showsVerticalScrollIndicator = YES;
// add label
UILabel *lblTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 25, self.bgView.frame.size.width, 46)]autorelease];
// If I change the 25 to 235 the label will come under the screen but still I cannot scroll.
lblTitle.textAlignment = NSTextAlignmentCenter; // Align the label text in the center
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.text = self.initialTitle;
lblTitle.textColor = self.initialTitleColor;
lblTitle.font = [UIFont systemFontOfSize:31];
[scroll addSubview: lblTitle];
You have to make the scrollview's contentSize larger than the view:
[scroll setContentSize:CGSizeMake(200, 200)];
needs to be something like:
[scroll setContentSize:CGSizeMake(400, 400)];
Of course, make that the size of the content of whatever you're going to put the scroll view, and that way, it only scrolls if needs to.
For Scrolling - The scrollView contentSize should be larger then the scrollView frame.

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