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];
}
Related
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.
I have Created UIScrollview with Paging, But i want to add Date picker in some pages. But Date picker scroll not working.
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];
CGRect pickerFrame = CGRectMake(0,100,100,100);
UIDatePicker *myPicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
[myPicker addTarget:self action:#selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:myPicker];
[myPicker release];
[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;
Instead of adding the picker view to self.view, add it to self.scrollView
[self.scrollView addSubview:myPicker];
Or else, You can add picker view as input view to a text field,
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];
//CGRect pickerFrame = CGRectMake(0,100,100,100);
myTextField = [[UITextField alloc]initWithFrame:
CGRectMake(10, 100, 300, 30)]; //if you dont want to show the text field you can change the frame to CGRectMake(0, 0, 1, 1)
myTextField.borderStyle = UITextBorderStyleRoundedRect;
myTextField.textAlignment = NSTextAlignmentCenter;
myTextField.delegate = self;
[self.view addSubview:myTextField];
UIDatePicker *myPicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
[myPicker addTarget:self action:#selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
[myTextField setInputView:myPicker];
[self.scrollView addSubview:myTextField];
[myPicker release];
[self.scrollView addSubview:subview];
[subview release];
}
and when you want to show the picker, Call the below method [self showPickerView]
- (void)showPickerView
{
[myTextField becomeFirstResponder];
}
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];
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;
}
}
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;
}