Create multiple UIImageViews programmatically - ios

I want to create multiple UIImageViews programmatically with different tags and add them as subview of my main view.
I have a property of my UIImageView in header:
#property (strong, nonatomic) UIImageView *grassImage;
then i'm trying to create multiple views:
for (int i=0;i<13;i++){
grassImage = [[UIImageView alloc] init];
int randNum = arc4random() % 320; //create random number for x position.
[grassImage setFrame:CGRectMake(randNum, 200.0, 50.0, 25.0)];
[grassImage setTag:i+100];
[grassImage setImage:[UIImage imageNamed:#"grass"]];
[self.view addSubview:grassImage];
}
But when I'm trying to access this image views using tag, I'm getting only last tag - 112.
My question - how I can access this views correctly, using their tag?
Similar questions:
Add a multiple buttons to a view programmatically, call the same method, determine which button it was

You are only getting the last one because you are recreating the same view all the time.
Get rid of that variable, and add your views like this:
for (int i=0;i<13;i++){
UIImageView *grassImage = [[UIImageView alloc] init];
int randNum = arc4random() % 320; //create random number for x position.
[grassImage setFrame:CGRectMake(randNum, 200.0, 50.0, 25.0)];
[grassImage setTag:i+100];
[grassImage setImage:[UIImage imageNamed:#"grass"]];
[self.view addSubview:grassImage];
}
And to get the views:
UIImageView *imgView = [self.view viewWithTag:110];

Since you are re-creating the same image again and again, if you access grassImage it will give you the last imageview you created. Instead you can get the imageview like this.
for (UIImageView *imgView in self.view.subviews) {
if ([imgView isKindOfClass:[UIImageView class]]) {
NSLog(#"imageview with tag %d found", imgView.tag);
}
}

Use this code for getting subview with a particular tag,
UIImageView *imgViewRef = (UIImageView *)[self.view viewWithTag:TAG_NUMBER];

Related

Make UIImages inside UIView Grow When Zooming

I have a UIView Just like images aligned in gridlike view.
Created using the code.
for (int i=_farmView.bounds.origin.x; i<_farmView.bounds.size.width; i+=originalBagWidth*2) {
for (int j=_farmView.bounds.origin.y; j<_farmView.bounds.size.height; j+=originalBagHeight*2) {
//adding bag
n+=1;
CGRect frame = CGRectMake(i, j, originalBagWidth, originalBagHeight);
UIView *v = [[UIView alloc] initWithFrame:frame];
UIImage *image = [UIImage imageNamed:#"bag"];
image = [self imageResize:image andResizeTo:v.bounds.size];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[v addSubview:imageView];
[_farmView addSubview: v];
[v setBackgroundColor: [UIColor darkGrayColor]];
}
}
And I have added proper Code for zooming the UIView using Pinch Recognizers. Now when user zooms the UIView The Image Becomes unclear.
I want to grow the pixel size when zooming the UIView. How can i
achive this functionality
Note: The number of UIImages inside UIView and size of UIView is decided at runtime and cannot be predicted.

Align multiple views vertically

I've to make something that looks like these:
I have a NSArray of walking and car objects.
Car class has a NSString property which contains these numbers.
How can I do this ? Thanks.
You could create UIImageViews and UILabels programatically and line them up in a cell/view whatever.
You could create a UIView as a container, with an UIImageView on the left and a UITextView on the right. This would be the template for the car object. Your walking object would just need a simple UIImageView. Then calculate the point you want the first UIImage to start at and add the next container at the end of the previous element. you could also create a template of your arrow, this way you just would need to connect the elements like you want. It should look like this
CGFloat fHeightImage = 20.0f;
UIView *view =
[[UIView alloc] initWithFrame:CGRectMake(0, 0,
[UIScreen mainScreen].bounds.size.width,
fHeightImage)];
UIImageView *imageViewCar =
[[UIImageView alloc] initWithFrame:CGRectMake(0, 0,
fHeightImage,
fHeightImage)];
[imageViewCar setImage:[UIImage imageNamed:#"walking"]];
[view addSubview:imageViewCar];
UIImageView *imageViewArrow =
[[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(imageViewCar.frame),
CGRectGetHeight(imageViewCar.frame),
fHeightImage,
fHeightImage)];
[imageViewArrow setImage:[UIImage imageNamed:#"arrow"]];
[view addSubview:imageViewArrow];

iOS Calc X position to place elements on a View

I want to add a Label and an Image in titleView of my NavigationItem. I am adding UILabel and UIImageView to a UIView and setting it as titleView of the navigation Item. Things are being added, but I am not able to calculate the length of label and place image next to it.
My code is :-
// Title Name + Image
UIView *titView = [[UIView alloc] initWithFrame:self.navigationItem.titleView.bounds];
self.navigationItem.titleView = titView;
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(-10, -20, 150, 30)];
title.text = #"Bunny ";
[title setTextColor:[UIColor orangeColor]];
[titView addSubview:title];
float x2 = 30; //+ title.bounds.size.width;
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"return" ]];
img.frame = CGRectMake(x2, -20, img.bounds.size.width, 30);
[titView addSubview:img];
I am looking for some way to calc the width text and set accordingly the width of the label. Right now I have set the width of the label as 150. And secondly, calc the x position to place the image just next to the label.
Tried many ways, but nothing works as expected. How can I achieve this ? Can you he give some guidelines such that regardless of length of text of label, things works as expected and UIView gets placed in the center of navigation item.
Any help is highly appreciated. Thanks.
You can call
[title sizeToFit];
after setting its properties and it will size itself to match the contained string.
Set the imageViews x origin to
CGRectGetMaxX(title.frame) + desiredSpacing;
And it could help to subclass UIView to achieve the final results by overwriting layoutSubviews and placing your views there, since the titleView's frame can be adjusted by the navigationBar...basically like this:
#implementation TitleView{
UILabel *_titleLabel;
UIImageView *_img;
}
- (id)initWithTitle:(NSString *)title andImage:(UIImage *)image
{
self = [super init];
if (self) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.text = title;
[_titleLabel setTextColor:[UIColor orangeColor]];
[_titleLabel sizeToFit];
[self addSubview:_titleLabel];
_img = [[UIImageView alloc] initWithImage:image];
[self addSubview:_img];
}
return self;
}
- (void)layoutSubviews{
CGFloat spacingBetweenTextAndImage = 0;
CGFloat width = CGRectGetWidth(_titleLabel.frame)+CGRectGetWidth(_img.frame)+spacingBetweenTextAndImage;
CGFloat x = (CGRectGetWidth(self.bounds)-width)/2;
_titleLabel.frame = CGRectMake(x, (CGRectGetHeight(self.bounds)-CGRectGetHeight(_titleLabel.bounds))/2, CGRectGetWidth(_titleLabel.bounds), CGRectGetHeight(_titleLabel.bounds));
x+=CGRectGetWidth(_titleLabel.bounds)+spacingBetweenTextAndImage;
_img.frame = CGRectMake(x, (CGRectGetHeight(self.bounds)-CGRectGetHeight(_img.bounds))/2, CGRectGetWidth(_img.bounds), CGRectGetHeight(_img.bounds));
}
#end
use in your viewController:
UIView *titleView = [[TitleView alloc] initWithTitle:#"Bunny" andImage:[UIImage imageNamed:#"return" ]];
self.navigationItem.titleView = titleView;

Adding a UIImageView as a subview of UILabel, image not appearing

So, when I do this with a regular old view:
UIView *topBlock = [[UIView alloc] initWithFrame:CGRectMake(0,-frameSize.height, frameSize.width, frameSize.height/2)];
[viewController.view addSubview:topBlock];
topBlock.autoresizesSubviews = NO;
topBlock.clipsToBounds = YES;
UIImage *topImage = [UIImage imageNamed:#"BloktLayout"];
UIImageView *topImageView = [[UIImageView alloc] initWithImage:topImage];
topImageView.frame = viewController.view.frame;
[topBlock addSubview:topImageView];
I get the nice old image where I want it, in the top view. But the middle view is a UILabel, and when I try the same thing:
UILabel *midBar = [[UILabel alloc] initWithFrame:CGRectMake(midBarOrigin.x, midBarOrigin.y, midBarWidth, midBarHeight)];
midBar.text = #"Blokt";
midBar.textColor = [UIColor blackColor];
midBar.textAlignment = NSTextAlignmentRight;
midBar.font = [UIFont fontWithName:#"HelveticaNeue-UltraLight" size:80.0f];
[viewController.view addSubview:midBar];
midBar.autoresizesSubviews = NO;
midBar.clipsToBounds = YES;
UIImage *midImage = [UIImage imageNamed:#"BloktLayout"];
UIImageView *midImageView = [[UIImageView alloc] initWithImage:midImage];
midImageView.frame = viewController.view.frame;
[midBar addSubview:midImageView];
I don't see any image at all in the UILabel. Any help?
Seems like the issue is related to your frames.
Tough to say without additional info. Can you post viewController.view.frame, frameSize, and midBarOrigin / midBarWidth / midBarHeight?
In the second codeblock, midBar.clipsToBounds == YES, but it looks like the midImageView.frame is likely very different / outside of midBar.frame in which case it wouldn't be visible.
Edit Some screenshots would help but aren't necessary
Edit 2 Note that subviews' origin points are always relative to the coordinate system of their superview, never relative to the coordinate system of any other view in the view heierarchy. This is likely the heart of the issue here. If you do want to convert CGPoints or CGRects from one coordinate system to another there are methods on UIView such as convertRect: and convertPoint: etc.
Interface Builder doesn't even let you add a control inside of a UILabel.
Instead, if you wish to group multiple controls, you can add them both as subviews of a UIView.
In other words, your image view and label can share the same superview, but the image view cannot be a subview of the label.
If they share the same superview, you can position the image view behind the label, and it should appear "through" the label as long as the label's background is clear.
Simple Way to do.....
You can add UIImageView, UILabel as subview of cell.textLabel
UIImageView *statusImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 4, 8, 8)];<br/>statusImage.backgroundColor = [UIColor redColor];<br/>
statusImage.layer.cornerRadius = 4;<br/>
statusImage.clipsToBounds = YES;<br/>
[cell.textLabel addSubview:statusImage];<br/>
UILabel *Lbl = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, cell.textLabel.frame.size.width - 15, cell.textLabel.frame.size.height)];<br/>
Lbl.text = #"xyz";<br/>
[cell.textLabel addSubview:Lbl];<br/>
I just had this problem as well.
I found that ImageView was behind the label.
When I replaced label with UIView, it works properly.

UIImageView shows outside its parents frame

I'm developing a iOs app for iPad and I'm implementing a UIImageView inside a UIView. The problem is that if the position of the image view has a position out of the UIView that it is inside, you can see it. The code:
UIView *vistafunda = [[UIView alloc] initWithFrame:CGRectMake(512/2, 48, 525, 651)];
[self.view addSubview:vistafunda];
vistafunda.backgroundColor = [UIColor darkGrayColor];
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"hola.png"]];
img.center = CGPointMake(12, 200);
[vistafunda addSubview:img];
I would like that the image view was always under the view, so if the image is outside the view you could not see it.
Thanks!
Just make the containing view clip it's children
vistafunda.clipsToBounds = YES;

Resources