XCODE Obj-C add UiimageView programatically in a for - ios

So i need to add programatically 3 images using a for loop this is my code , it's not correct, i just tryed.
for(int i = 1; i < 4; i++){
UIImageView *[i] = [[UIImageView alloc] initWithFrame:CGRectMake([i]*50, 50, 250, 250)];
[self.view addSubview:[i]];
}
where [i]; will be 1 , 2 and 3 any help would be appreciated. Thanks

You syntax is wrong. This is more what you need.
for (int i=0;i<3;i++){
UIImageView *image=[[UIImageView alloc] initWithFrame:CGRectMake(i*50, 50, 250, 250)];
[self.view addSubView:image];
}
However, this is not much use to you as your images are not referenced from anywhere so you can not populate or adjust them easily.

Change the variable name from [i] to some name like imageToAdd.

CGFloat xMarging = 0;
CGFloat imageOriginX = 0;
CGFloat imageOriginY = 0;
CGFloat imageWidth = 250;
CGFloat imageHeight = 250;
NSInteger numerOfImages = 3;
for (NSInteger i = 0; i < numerOfImages; i++){
[self.view addSubview:[[UIImageView alloc] initWithFrame:CGRectMake(xMarging + i * imageWidth,
imageOriginY,
imageWidth,
imgageHeight)]];
}
assuming you want them side by side as in [][][]

Related

Display some images on album cover in iOS

i want to display some images on album cover on my app like bellow image.
EDIT:-
i want to display this view in UICollectionViewCell and collectionView scrollDirection Horizontal with paging enabled.
any help will be appreciated. and thanks in advance.
I do not know if you understand the question correctly.
If you want to do what I see on the picture, so try this:
UIView *cover = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 500, 500)];
cover.backgroundColor = [UIColor blueColor];
int width = 200;
int height = 350;
int posx = 100;
int posy = 50;
for (int i = 0; i < 10; i++) {
//craete images
UIView *imagebg = [[UIView alloc] initWithFrame:CGRectMake(posx + arc4random_uniform(20), posy + arc4random_uniform(20), width, height)];
imagebg.backgroundColor = [UIColor whiteColor];
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, width - 40, height - 50)];
image.image = [UIImage imageNamed:#"loading-screen.png"];
[imagebg addSubview:image];
//rotate
double rads = arc4random_uniform(100)/10;
CGAffineTransform transform = CGAffineTransformRotate(imagebg.transform, rads);
imagebg.transform = transform;
//add as subview
[cover addSubview:imagebg];
}
[self.view addSubview:cover];

How can I access imageView with tag inside a scrollView?

-(void) addImageviewsToscrollview {
int cx = 0;
for (int i = 0; i < 4; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"placeholder.png"]];
CGRect rect = imageView.frame;
rect.size.height = 100;
rect.size.width = 100;
rect.origin.x = cx;
rect.origin.y = 0;
imageView.tag = i;
imageView.frame = rect;
[self.imageScrollview addSubview:imageView];
cx += imageView.frame.size.width+25;
}
[self.imageScrollview setContentSize:CGSizeMake(cx, [self.imageScrollview bounds].size.height)];
}
Adding ImageView to the ScrollView. Application is used only for iPad and I'm using storyboard. Should I create a new class and a new UIViewController?
If you want access any UIImageView with any tag, use viewWithTag:
UIImageView *imageView = (UIImageView *)[self.imageScrollview viewWithTag:1];
Just change number 1 with your tag.
Try adding tag by starting 1
imageView.tag = i+1;

Create a dynamic row of UITextfields

I would like some help creating a dynamic row of UITextfields, I have been trying to do it myself with some success but mostly failure.
I set a number of rows between 1 - 13 and I would like to create UITextfields that fit inside the width of an iPhone frame width evenly spaced with an even gap to the left and right.
Here is what I have done so far:
- (void) displayViews {
// add scrollview
htmlContainerScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 60.0, self.view.frame.size.width, 293.0)];
NSInteger viewcount = [axisString integerValue];
color = 200;
for(int i = 0; i< viewcount; i++) {
color = color + 20;
NSLog(#"%f", color);
CGFloat y = i * 91;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, y,self.view.frame.size.width, 90.0)];
view.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:(1.0/i)];;
[htmlContainerScrollView addSubview:view];
int cutCount = [cutsString integerValue];
int positions = (self.view.frame.size.width/cutCount);
for (int i = 0; i < cutCount; i++) {
//first one
UITextField *cutField = [[UITextField alloc] initWithFrame:CGRectMake((positions*i)+(20/2), 25, 20, 20)];
cutField.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
cutField.font = [UIFont fontWithName:#"Helvetica-Bold" size:25];
cutField.backgroundColor=[UIColor whiteColor];
[view addSubview:cutField];
}
}
htmlContainerScrollView.contentSize = CGSizeMake(self.view.frame.size.width, 91 *viewcount);
[self.view addSubview:htmlContainerScrollView];
}
The biggest problem with mine is that it does not evenly space them; they are always to the left.
change your cutField's frame x coordinate value calculation from
(positions*i)+(20/2)
to
((positions*i)-(20/2)+(positions/2)).
that should do the trick.
it's just a matter of the x coordinate calculation and i am sure there is room to optimize that a bit.

UIViews are blank when added to UIScrollView

I'm trying to add the Views of a ViewControllerClass to a ScrollView.
The problem is that only the first page is showing up, the other ones are blank.
This is my code
for (int i = 0; i < 8; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[imageArray2 addObject:[[WeatherViewController alloc] initWithNibName:#"WeatherViewController" picname:[NSString stringWithFormat:#"s%#.png", [[xmlParser.weatherdata.timedata objectAtIndex:i] iconSymbol]] degree:#"0°" weathertimedata:[xmlParser.weatherdata.timedata objectAtIndex:i] town:[xmlParser.weatherdata town]]];
}
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 8, scrollView.frame.size.height);
for(WeatherViewController *iv in imageArray2)
{
[self.scrollView addSubview:iv.view];
}
Any advice?
You have to assign your calculate frame to your WeatherViewController's view on your loop.
Try this
for (int i = 0; i < 8; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
WeatherViewController *myWeatherVC = [[WeatherViewController alloc] initWithNibName:#"WeatherViewController" picname:[NSString stringWithFormat:#"s%#.png", [[xmlParser.weatherdata.timedata objectAtIndex:i] iconSymbol]] degree:#"0°" weathertimedata:[xmlParser.weatherdata.timedata objectAtIndex:i] town:[xmlParser.weatherdata town]]
myWeatherVC.view.frame = frame;
[imageArray2 addObject:myWeatherVC];
[self.scrollView addSubview:myWeatherVC.view];
}
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 8, scrollView.frame.size.height);
Just assign the frame like this and it will work.
for (int i = 0; i < 8; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
WeatherViewController *thisView = [[WeatherViewController alloc] initWithNibName:#"WeatherViewController" picname:[NSString stringWithFormat:#"s%#.png", [[xmlParser.weatherdata.timedata objectAtIndex:i] iconSymbol]] degree:#"0°" weathertimedata:[xmlParser.weatherdata.timedata objectAtIndex:i] town:[xmlParser.weatherdata town]]
[thisView.view setFrame:frame];
[imageArray2 addObject:thisView];
}
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 8, scrollView.frame.size.height);
for(WeatherViewController *iv in imageArray2)
{
[self.scrollView addSubview:iv.view];
}
Hope this will help you.

How to add UIButtons programatically using for loop in iOS?

I want to create 15 UIButtons progaramatically using for loop. I want to arrange buttons like matrix with rows and colums, say 3 rows and 5 columns. The buttons height is 50 and width is 80. I can set y,width,height coordinates. But I face problem only with x coordinate. Tell me the logic for setting x coordinate.
Thanks in advance.
float x = 10;
float y = 10;
float width = 50;
float height = 50;
for(int i =0;i<15;i++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(x, y, width, height)];
[btn setTitle:[NSString stringWithFormat:#"B%d",i+1] forState:UIControlStateNormal];
[self.view addSubview:btn];
x = x + width + 20;
if((i+1)%3==0)
{
x = 10;
y = y + height + 20;
}
}
Just replace x and y and also set height and width in above code.
int row = 0;
int column = 0;
for (int i = 0; i < 15; i++)
{
if((row%3 == 0) && (row > 0))
{
row = 0;
column++;
}
else{
row++;
}
CGRect btnFrame = CGRectMake(row*80+10, column*50+10, 80, 50);//your button frame
UIButton *btnTemp = [UIButton buttonWithType:UIButtonTypeCustom];
[btnTemp setFrame:btnFrame];
[self.view addSubView:btnTemp];
}
Here you go:
CGFloat marginX = 10.f;
CGFloat marginY = 5.f;
CGRect buttonFrame = CGRectMake(0., 0., 80., 50.f);
for(NSUInteger row = 0; row < 5; row++) {
for(NSUInteger column = 0; column < 3; column++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = buttonFrame;
//... more button settings
[self.view addSubview:button];
buttonFrame.origin.x += marginX;
}
buttonFrame.origin.x = 0.;
buttonFrame.origin.y += marginY;
}

Resources