Scrollview not creating through programmatically - ios

I want to creat a scrollview programmatically but the scroll view is not creating the below is my code i want to display images in that scrollview.
fscroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view1.frame.size.width, self.view1.frame.size.height)];
fscroll.contentSize = CGSizeMake(320, 400);
fscroll.backgroundColor = [UIColor blackColor];
fscroll.showsHorizontalScrollIndicator = YES;
[view1 addSubview:fscroll];
int X=0;
for (int i = 0; i < [images count]; i++)
{
imageView = [[UIImageView alloc] initWithFrame: CGRectMake(X, 0, 320, 480)];
imageView.backgroundColor = [UIColor blackColor];
[imageView setImage: [images objectAtIndex:[sender tag]]];
[imageView addSubview:fscroll];
X = X + imageView.frame.size.height;
if(X > 320)
self.fscroll.contentSize = CGSizeMake(X, 134);
if(X > 320)
self.fscroll.contentSize = CGSizeMake(X, 134);
}

X = X + imageView.frame.size.height;
Did you mean to do this?
X = X + imageView.frame.size.width;
EDIT: Also, are you sure view1 has the right frame set before your UIScrollView is initialized?

This is wrong: [imageView addSubview:fscroll]; Presumable you want to do [fscroll addSubview:imageView];

Simple Method: You can created multiple times if you need means
- (void)scrollView
{
int x = 0; //scrollview width
int y = 10;//scrollview height
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);
x=x+55;
}
}

Related

How to Set the View Using for Loop?

I have to design screen like this.The Essential Category Contains 4 items,then Kitchen category Contains 8 items,then utility category contains 11 items.How to do this with reusability?
This is My Code which i tried:
- (void)buildAppliancesView
{
for (int i=0; i<5; i++)
{
[self addAppliancesCategoriesLabel:i];
[self addApplianceCategoryView:i];
[self addIndividualAppliances:i];
}
}
- (void)addAppliancesCategoriesLabel:(int)y
{
label = [[UILabel alloc] initWithFrame:CGRectMake(0,(_appliancesHeaderLabel.frame.origin.y+_appliancesHeaderLabel.frame.size.height)+y*150+10, 0, 0)];
NSLog(#"y=>%f",label.frame.origin.y);
label.text =[NSString stringWithFormat:#"%#",[APPLIANCESCATEGORIESLABEL_ARRAY objectAtIndex:y]];
[label sizeToFit];
[_scrollView addSubview:label];
}
- (void)addApplianceCategoryView:(int)y
{
applianceCategoryView = [[UIView alloc]initWithFrame:CGRectMake(5, label.frame.origin.y+label.frame.size.height+5, self.view.frame.size.width-10, 70)];
[applianceCategoryView setBackgroundColor:[UIColor grayColor]];
[_scrollView addSubview:applianceCategoryView];
}
- (void)addIndividualAppliances:(int)y
{
for (int i=0; i<4; i++)
{
UIView *applianceView = [[UIView alloc]initWithFrame:CGRectMake(5+((self.view.frame.size.width/4)+5)*y, _appliancesHeaderLabel.frame.origin.y+_appliancesHeaderLabel.frame.size.height+label.frame.size.height+10, 50, 50)];
NSLog(#"xVAlue==>>%f",applianceView.frame.origin.x);
[applianceView setBackgroundColor:[UIColor redColor]];
UITapGestureRecognizer *singleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(changecolor)];
[applianceView addGestureRecognizer:singleTap];
[applianceCategoryView addSubview:applianceView];
}
}
Check this - sample code for creating views in loop.
int xPosition = 0;
int yPosition = 0;//set your y origin
int height = 150; // sample value
int width = (NumberOfItems.count)/4;
for (int i = 0; i < NumberOfItems.count; i++) {
UIView * view = [[UIView alloc] initFrame:CGRectMake(xPosition, yPosition, width, height)];
[self.scrollView addSubview:view];
xPosition += width;
if(xPosition == 3*width){
xPosition=0;
yPosition = yPosition + height; // set as per your requirement
}
}
Using UIScrollview for this will be better.
OR
Here is the sample code for your scenario.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// UITapGestureRecognizer *singleTap=[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapDetected:)];
// singleTap.numberOfTapsRequired=1;
UIScrollView *ScrollVc = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
ScrollVc.delegate = self;
ScrollVc.showsHorizontalScrollIndicator = NO;
[ScrollVc setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:ScrollVc];
ScrollVc.indicatorStyle = UIScrollViewIndicatorStyleWhite;
// [ScrollVc addGestureRecognizer:singleTap];
NSArray *essentialsArr = #[#"1",#"2",#"3"];
NSArray *KitchenArr = #[#"1",#"2",#"3",#"4",#"5",#"6"];
NSArray *abcArr = #[#"1",#"2",#"3",#"4"];
NSArray *arr = [NSArray arrayWithObjects:essentialsArr,KitchenArr,abcArr, nil];
int yPosition = 0;//set your y origin
int yPositionOfMain = 0;//set your y origin
for (int k = 0; k < arr.count; k++) {
int xPosition = 0;
int height = 100; // sample value
int width = ([[UIScreen mainScreen] bounds].size.width)/4;
UIView *mainview = [[UIView alloc] init];
mainview.backgroundColor = [UIColor yellowColor];
[ScrollVc addSubview:mainview];
for (int i = 0; i < [[arr objectAtIndex:k] count]; i++) {
if (i==0) {
xPosition=0;
yPosition= 0;
}
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(xPosition + 10, yPosition, width-20, height)];
view.backgroundColor = [UIColor redColor];
[mainview addSubview:view];
xPosition += width;
if(xPosition == 4*width && i!=[[arr objectAtIndex:k] count]-1){
xPosition=0;
yPosition = yPosition + height + 10; // set as per your requirement
}
}
mainview.frame = CGRectMake(0, yPositionOfMain, [[UIScreen mainScreen] bounds].size.width, yPosition + height + 10);
yPositionOfMain = yPositionOfMain + mainview.frame.size.height + 20;
}
if ([UIScreen mainScreen].bounds.size.height==736)// iPhone 6 Plus 66 Navigation bar
{
[ScrollVc setContentSize:CGSizeMake(320,950)];
}
else if ([UIScreen mainScreen].bounds.size.height==667)// iphone 6 44 Navigation bar
{
[ScrollVc setContentSize:CGSizeMake(320,900)];
}
else if ([UIScreen mainScreen].bounds.size.height==568)// iphone 5, 5C, 5S 44 Navigation bar
{
[ScrollVc setContentSize:CGSizeMake(320,800)];
}
else // iphone 4, 4S 44 Navigation ba
{
[ScrollVc setContentSize:CGSizeMake(320,750)];
}
}
Please change the necessary values as per your requirements.
Hope this helps.

ScrollView for images Objective C (iOS)

I am trying to create a welcome page for my app (with screenshots).
I have taken some samples and made it into a text scroll. I have tried for weeks without any success. Thanks
int numberOfPages = 5;
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
someScrollView.pagingEnabled = YES;
someScrollView.contentSize = CGSizeMake(numberOfPages * someScrollView.frame.size.width, someScrollView.frame.size.height);
[self.view addSubview:someScrollView];
for (int i = 0; i < numberOfPages; i++) {
UILabel *tmpLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * someScrollView.frame.size.width + 20,
20,
someScrollView.frame.size.width - 40,
20)];
tmpLabel.textAlignment = UITextAlignmentCenter;
tmpLabel.text = [NSString stringWithFormat:#"THIS ACTUALLY WORKS!! %d", i];
[someScrollView addSubview:tmpLabel];
}
Here is my failed attempt to do with images:
int numberOfPages = 5;
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
someScrollView.pagingEnabled = YES;
someScrollView.contentSize = CGSizeMake(numberOfPages * someScrollView.frame.size.width, someScrollView.frame.size.height);
[self.view addSubview:someScrollView];
for (int i = 0; i < numberOfPages; i++) {
UIImageView *tmpLabel = [[UIImageView alloc] initWithFrame:CGRectMake(i * someScrollView.frame.size.width + 20,
20,
someScrollView.frame.size.width - 40,
20)];
tmpLabel.imageAlignment = UIImageAlignmentCenter;
tmpLabel.text = [NSString stringWithFormat:#"THIS ACTUALLY WORKS!! %d", i];
[someScrollView addSubview:tmpLabel];
}
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:subview1.frame];
if (i == 0) {
imageView1.image = [UIImage imageNamed:#"openingScreen1.png"];
}else if (i == 1){
imageView1.image = [UIImage imageNamed:#"openingScreen2.png"];
}else if (i == 2){
imageView1.image = [UIImage imageNamed:#"openingScreen3.png"];
}else {
imageView1.image = [UIImage imageNamed:#"openingScreen4.png"];
}
I also might need to remove the word "Parse" unless the images above sit on top of it? I cannot locate the word.
try this
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
self.view.frame.size.width,
self.view.frame.size.height)];
someScrollView.pagingEnabled = YES;
[someScrollView setAlwaysBounceVertical:NO];
//setup internal views
NSInteger numberOfPages = 5;
for (int i = 0; i < numberOfPages; i++)
{
CGFloat xOrigin = i * self.view.frame.size.width+10;
UILabel *tmpLabel = [[UILabel alloc] initWithFrame:CGRectMake(xOrigin,70,self.view.frame.size.width,25)];
tmpLabel.textAlignment = NSTextAlignmentCenter;
tmpLabel.text = [NSString stringWithFormat:#"THIS ACTUALLY WORKS!! %d", i+1];
[someScrollView addSubview:tmpLabel];
// calculate the width
UIImageView *image = [[UIImageView alloc] initWithFrame:
CGRectMake(xOrigin, tmpLabel.frame.size.height + tmpLabel.frame.origin.y + 10,
self.view.frame.size.width,
self.view.frame.size.height)]; // customize your width, height and y co ordinates
image.image = [UIImage imageNamed:[NSString stringWithFormat:
#"openingScreen%d.jpg", i+1]];
image.contentMode = UIViewContentModeScaleAspectFit;
[someScrollView addSubview:image];
}
//set the scroll view content size
someScrollView.backgroundColor = [UIColor blueColor];
someScrollView.contentSize = CGSizeMake(self.view.frame.size.width *
numberOfPages,
self.view.frame.size.height);
//add the scrollview to this view
[self.view addSubview:someScrollView];
the sample OutPut is
here I attached the sample Project also, please use this
Please change your code to something like
int numberOfPages = 5;
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
someScrollView.pagingEnabled = YES;
//set the scroll view delegate to self so that we can listen for changes
someScrollView.delegate = self;
someScrollView.contentSize = CGSizeMake(numberOfPages * someScrollView.frame.size.width, someScrollView.frame.size.height);
[self.view addSubview:someScrollView];
for (int i = 1; i <= numberOfPages; i++) {
//set the origin of the sub view
CGFloat myOrigin = i * self.view.frame.size.width;
//get the sub view and set the frame
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width, someScrollView.frame.size.height)];
imageView1.image = [UIImage imageNamed: [NSString stringWithFormat:#"openingScreen%d.png",i ]];
//add the subview to the scroll view
[someScrollView addSubview:imageView1];
}
Add scroll view Delegate methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
}
//dragging ends, please switch off paging to listen for this event
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *) targetContentOffset NS_AVAILABLE_IOS(5_0)
{
//find the page number you are on
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
NSLog(#"Dragging - You are now on page %i",page);
}

How to add images in nested UIScrollview

I need to add images in scroll view multiple times. Am created a scrollview but images not append correctly in that.
My code is here :
-(void)sampleScroll
{
int x = 10;
int y = 20;
mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
mainScrollView.contentSize = CGSizeMake(50, (y + 95) * 5);
// further configure
[self.view addSubview: mainScrollView];
images = [[NSMutableArray alloc] initWithObjects:#"image0.jpg",#"image1.jpg",#"image2.jpg",#"image3.jpg", nil];
for(int i=0; i<[images count]; i++)
{
NSLog(#"%#",images);
UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(x, y, 250, 150)];
scrollview.showsVerticalScrollIndicator=YES;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
scrollview.backgroundColor = [UIColor whiteColor];
NSString *img = [images objectAtIndex:i];
NSLog(#"%#",img);
imageView.image = [UIImage imageNamed:#"image3.jpg"];
NSLog(#"%#",imageView.image);
scrollview.contentSize = CGSizeMake(1250,250);
[scrollview addSubview:imageView];
[mainScrollView addSubview:scrollview];
y=y+155;
//[self myscrollView];
}
}
Please give me a solution. Thanks in advance..
Frame adjustment needed,
int x = 10;
int y = 10;
UIScrollView * mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
mainScrollView.contentSize = CGSizeMake(mainScrollView.frame.size.width, mainScrollView.frame.size.height * COUNTS);
mainScrollView.pagingEnabled = YES;
for(int i = 0; i < COUNTS; i++)
{
UIScrollView * scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(x, y, mainScrollView.frame.size.width - 20.0, mainScrollView.frame.size.height - 20.0)];
scrollview.showsVerticalScrollIndicator = YES;
scrollview.scrollEnabled = YES;
scrollview.userInteractionEnabled = YES;
scrollview.backgroundColor = [UIColor whiteColor];
UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(x,10.0, mainScrollView.frame.size.width - 40.0, mainScrollView.frame.size.height - 40.0)];
imageView.image = [UIImage imageNamed:#"owl.jpg"];
[scrollview addSubview:imageView];
[mainScrollView addSubview:scrollview];
y += mainScrollView.frame.size.height;
}
[self.view addSubview:mainScrollView];

Make UIScrollView Loop iOS

I apologize in advance for a basic question but I am new to iOS and Arrays but I would like to loop the UIScrollView that I made with the pictures in it once it gets to the end instead of stopping. Do I reset a variable back to 0 once it knows my pagecount = 3? How would i go about this? Thank you!
int PageCount = 3;
NSMutableArray *arrImageName =[[NSMutableArray alloc]initWithObjects:#"1.png",#"2.png",#"3.png", nil];
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
Scroller.scrollEnabled=YES;
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
[self.view addSubview:Scroller];
int width=Scroller.frame.size.width;
int xPos=0;
for (int i=0; i<PageCount; i++)
{
UIImageView *ImgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPos, 0, Scroller.frame.size.width, Scroller.frame.size.height)];
[ImgView setImage:[UIImage imageNamed:[arrImageName objectAtIndex:i]]];
[Scroller addSubview:ImgView];
Scroller.contentSize = CGSizeMake(width, 0);
width +=Scroller.frame.size.width;
xPos +=Scroller.frame.size.width;
}

UIScrollView With Pictures Generated

I have set up a UIScrollView and loaded in pictures and set it equal to the view with some offset between the pictures. Can someone possibly explain what I might have done wrong? It shows the first picture just fine but wont let me scroll left and right to see the next ones. Do I need a gesture recognizer with the new XCode to make this work?
- (void)viewDidLoad
{
[super viewDidLoad];
int PageCount = 3;
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
Scroller.contentSize = CGSizeMake(PageCount = Scroller.bounds.size.width, Scroller.bounds.size.height);
CGRect ViewSize = Scroller.bounds;
UIImageView *ImgView = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView setImage:[UIImage imageNamed:#"1.png"]];
[Scroller addSubview:ImgView];
ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
UIImageView *ImgView2 = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView2 setImage:[UIImage imageNamed:#"2.png"]];
[Scroller addSubview:ImgView2];
ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
UIImageView *ImgView3 = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView3 setImage:[UIImage imageNamed:#"3.png"]];
[Scroller addSubview:ImgView3];
[self.view addSubview:Scroller];
}
#end
You need to manually set the contentSize of your ScrollView to the maximum rectangle which fits all the ImageViews you added. For example : Lets say you want to scroll right and left and you have 4 views with each view having width 100 and you have offset of 10 in x direction. Then after adding all the 4 views to your scrollView, you will have to make the contentSize as below :
scrollView.contentSize = CGSizeMake( 10 + (100 + 10)*4 , scrollView.contentSize.y );
This will make the scrollView scrollable and you will see all the views.
Try this
int PageCount = 3;
NSMutableArray *arrImageName =[[NSMutableArray alloc]initWithObjects:#"1.png",#"2.png",#"3.png", nil];
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
Scroller.scrollEnabled=YES;
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
[self.view addSubview:Scroller];
int width=Scroller.frame.size.width;
int xPos=0;
for (int i=0; i<PageCount; i++)
{
UIImageView *ImgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPos, 0, Scroller.frame.size.width, Scroller.frame.size.height)];
[ImgView setImage:[UIImage imageNamed:[arrImageName objectAtIndex:i]]];
[Scroller addSubview:ImgView];
Scroller.contentSize = CGSizeMake(width, 0);
width +=Scroller.frame.size.width;
xPos +=Scroller.frame.size.width;
}

Resources