How to Set the View Using for Loop? - ios

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.

Related

Horizontal UIScrollView swipe left and right doesn't work after long gesture?

I have a scrollview. It behaves as a slider.(Left to right, Right to left). If I swipe left or swipe right, it works. No problem. But If firstly I make long press and then I try to swipe left or right, it doesn't work.
- (void)initCarouselScroll
{
[self.carouselScroll removeFromSuperview];
self.carouselScroll = nil;
self.carouselScrollHeight = self.view.frame.size.height;
self.carouselScrollWidth = self.view.frame.size.width;
self.carouselScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.carouselScrollWidth, self.carouselScrollHeight)];
self.carouselScroll.backgroundColor = [UIColor clearColor];
self.carouselScroll.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.carouselScroll.tag = 500;
self.carouselScroll.showsHorizontalScrollIndicator = NO;
self.carouselScroll.directionalLockEnabled = YES;
self.carouselScroll.showsVerticalScrollIndicator = NO;
self.carouselScroll.pagingEnabled = YES;
self.carouselScroll.delegate = self;
self.carouselScroll.contentSize = CGSizeMake(self.carouselScrollWidth * [self.bannerList count], self.carouselScrollHeight);
[self.carouselScroll setContentOffset:CGPointZero];
[self.view addSubview:self.carouselScroll];
[self.view sendSubviewToBack:self.carouselScroll];
NSUInteger maxLoad = 3;
if ([self.bannerList count] < maxLoad) {
maxLoad = [self.bannerList count];
}
for (int i = 0; i < maxLoad; i++) {
[self loadImageFor:i];
}
[self updateSelectedDotImage:0];
}
- (void)loadImageFor:(int)index
{
if (index < 0 || index >= [self.bannerList count]) {
return;
}
AnyImageView* imageView = (AnyImageView*)[self.carouselScroll viewWithTag:index];
if (!imageView) {
Banner* banner = [self.bannerList objectAtIndex:(index)];
CGRect frame = CGRectMake(self.carouselScrollWidth * (index), 0, self.carouselScrollWidth, self.carouselScrollHeight);
NSString* imageUrl = banner.iphoneImage;
if (self.deviceType == DeviceTypeIpad) {
imageUrl = banner.ipadImage;
}
AnyImageView* imageView = [[AnyImageView alloc] initWithFrame:frame picPath:imageUrl isWhite:NO];
imageView.tag = index;
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(bannerButtonClicked:)];
UILongPressGestureRecognizer* longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleLongPressGestures:)];
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addGestureRecognizer:singleTap];
[button removeGestureRecognizer:longTap];
button.frame = frame;
button.tag = index;
button.backgroundColor = [UIColor clearColor];
[self.carouselScroll addSubview:imageView];
[self.carouselScroll addSubview:button];
}
}

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

Creating multiple views programmatically

Hello I would like to ask how to create multiple views programmatically and show them on the screen, I have tried this but there is something missing.
int x,y;
x= 0;
y=50;
for (int i=0; i<4; i++)
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, 300, 100)];
view.backgroundColor = [UIColor redColor];
[self.view addSubview: view];
y+=40;
}
int x,y,paddingX,widthOfView,HeightOfView;
x= 0;
y=50;
paddingX = 10;
widthOfView = 100;
HeightOfView = 100;
for (int i=0; i<4; i++){
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, widthOfView, HeightOfView)];
view.backgroundColor = [UIColor redColor];
[self.view addSubview: view];
y+= widthOfView + paddingX;
}
Following Code may help to you.
int x,y;
x= 0;
y=50;
for (int i=0; i<4; i++)
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, 300, 100)];
view.backgroundColor = [UIColor redColor];
view.layer.cornerRadius = 5.0f;
[self.view addSubview: view];
y+=140; //y for next view should be height of previous view and margin between view
}
int x,y;
x= 0;
y=50
for (int i=0; i<4; i++)
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, 30, 30)];
view.backgroundColor = [UIColor redColor];
y+=40;
[self.view addSubview: view];
}

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

Scrollview not creating through programmatically

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

Resources