IOS: change UIImageView with tag value - objective-c

I have 20 UIImageView and i want change their image; but I don't want to create 20 IBOutlet, and I want to use tag value to change the image; I set the tag value in interface builder and after? If I want to change image at Imageview number 15? How can I do?

UIImageView *imageView=(UIImageView *)[self.view viewWithTag:*givetag*];
[imageView setImage:[UIImage imageNamed:#"nameof yourimage"]];

If you use tags you can identify your tagged view (UIImageView is a subclass of UIView, which has the tag attribute) like this:
- (UIView *)viewWithTag:(NSInteger)tag
So if you call this method on your superview (which all the UIImageViews reside in), you should do it like this:
UIImageView *myImageView = (UIImageView *)[myAwesomeSuperview viewWithTag:15];
(Documentation I found using Google).
P.S: if you think it is too much work to add 20 IBOutlets, I recommend you create the UIImageViews programmatically as well. This way you will not need a xib file at all, write a small piece of code and have better maintenance with less effort.

SWIFT, April 2015:
func createUIImageViews()
{
for i in 0...99
{
var imageView:UIImageView = UIImageView(frame: CGRectMake(CGFloat(0), CGFloat(0), CGFloat(100), CGFloat(100)))
imageView.userInteractionEnabled = true
imageView.tag = i
imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "onImageViewTap:"))
self.view.addSubview(imageView)
}
}
func onImageViewTap(sender:UITapGestureRecognizer)
{
println("TAP: \(sender.view!.tag)")
}

Related

Change color of red button in edit table

i have searched a lot but i only find how to change the delete button. What i have to change is the color of the little rounded button. Is that possible?
That red button is not a button . It is a image.
You need to find for a subview in the UITableViewCell and when the subview is UITableViewCellEditControl, you cast the view as image view and you change the image in it.
You need to have a custom colored image similar to the red image.
for (UIView *subv in cell.subviews){
if ([NSStringFromClass([subv class]) isEqualToString:#"UITableViewCellEditControl"]) {
for (UIView *imgV in subv.subviews){ {
if(imgV isKindOfClass:[UIImageView class]]){
UIImageView *imgView = (UIImageView *)imgV;
imgView.image=[UIImage imageNamed:#"blue.png"];
imgV.backgroundColor=[UIColor blueColor];
NSLog(#"subview-subview name is %#",NSStringFromClass([imgV class]));
}
}
}
}
Please note that changing the private subviews is not a good practise. This method may not work in the next iOS update- credits to #rmaddy.
Another work around would be to design your own custom cell.

iOS 5 - Images within labels?

I'm sure there's going to be an easy answer for this, but I can't suss it out since I'm still new to iOS/Objective C.
I have a tableview with some cells that I'm populating with custom labels. I know how to fill the labels with things like
label.font
label.text
etc
But how can I put images inside of a label? Ideally, I need to find a loop that will let me put multiple images (like star ratings - some will have 1 star, others 3, etc) in a single label.
Help!
UILabel * tLable = [[UILabel alloc]init];
tLable setFrame:CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)];
for (int t = 0 ; t< numImages; t++) {
UIImageView * image = [UIImageView alloc]initWithFrame:CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)];
[image setImage:[UIImage imageNamed:<#(NSString *)#>]];
[tLable addSubview:image];
}
To use custom images use a UIImageView and add it as a subview of your cell or the label.
In order to put an actual image into a UILabel would be quite involved and would require you to subclass uilabel and override the drawrect method. Instead I would add a uiimageview to your cell where you want the image
You need to use UIImageView instead of images inside of a label.
Short answer: you can't. The UILabel class has no support for images. To achieve what you're looking for, will require you to build a custom view, which can be a composite of a series of UIImageViews.
There are a number of ways to achieve this:
create a NIB file with an empty view and layout your images inside it. Load an instance of it in your cellForRowAtIndexPath: method and add it to the appropriate place in your cell.
create a class that derives from UIView. Override the initWithFrame method and create,position and add the UIImageView instances to it in that method. Expose some properties that allow you to set the image contents externally.
Create a class that devices from UITableCellView and configure it appropriately.

Apply a shadow to an array of UIImageView

I have an array of UIImageViews. I want to apply a shadow to each of these images. I've used the code below:
- (void)awakeFromNib {
for (UIImageView *image in imagesJigsawPieces) {
image.layer.shadowColor = [UIColor blackColor].CGColor;
image.layer.shadowOffset = CGSizeMake(-1, -1);
image.layer.shadowOpacity = 1;
image.layer.shadowRadius = 5.0;
image.clipsToBounds = NO; //EDIT: I have also included this with no change
}
}
I have also included #import <QuartzCore/CALayer.h>.
I am not getting any errors but I am also not getting any shadows on my images.
Are you certain this code is being called? Have you placed a breakpoint in the for loop to verify?
-awakeFromNib is called only if you have a view (or whatever) in a nib file connected via IBOutlet to an ivar in your code. -awakefFromNib is called, in this case, instead of -initWithFrame: (or the like), an important distinction which I sometimes forget myself!

Add lots of views to NSScrollView

I'm trying to add one subview (view from an NSViewController) for every element in a dictionary to a NSScrollView to get kind of a tableview, but with much more flexibility over the cells.
Is it possible to place (programmatically) e.g. 100 subviews underneath each other so that you have to scroll down the NSScrollView to get to the last element?
The short answer is yes. I have done this before, and I assure you that it will work. Let me also assure you that it is not quite as simple as it looks ;)
The way to do this is to maintain one content view, in which you add all of your custom rows as subviews programmatically. Note that you will either have to resize the contentView before adding all of the rows, or you will have to turn off autoresizing for the row views. Set the documentView of your scrollView to this custom contentView, and you should be good to go.
Yes, simply initialize the views programmatically using (i.e.)
NSView *subView = [[NSView alloc] initWithFrame:CGRectMake(10,10,100,100)];
then add to the main using addSubview: method of the main view.
Remember to manually release the subview when you've done with it (that means, when you have added it to the main view).
As example you can do something like
int height x = 10, y = 10, width = 100, height = 100;
for(int i = 0;i<100;i++) {
NSView *subView = [[NSView alloc] initWithFrame:CGRectMake(x,y + height*i,width,height)];
[scrollView addSubview:subView];
[subView release];
}

How to put UISlider vertical?

I want to put UISlider in vertically. I have no idea about this, so please help me for this.
You have to do this programaticaly. Assuming your UISlider is bound to a variable called slider, add this code in your viewDidLoad method in ViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
CGAffineTransform trans = CGAffineTransformMakeRotation(M_PI * 0.5);
slider.transform = trans;
}
Let me know if you need any more help on this..
As of Xcode 4.2, you can sort of do the same in Interface Builder.
Create a slider
Show the "Identity Inspector"
Add a "User Defined Runtime Attribute"
Set the key path to "layer.transform.rotation.z", the type as "String" ("Number" won't allow floating point values) (possible since Xcode 5) and the value to "-1.570795" (-π/2).
Unfortunately, it will still appear as horizontal in Interface Builder.
But you can position the center and don't need to create a custom class, so it might be useful enough in some cases. The effect is the same as ravinsp's code.
Swift 3:
slider.transform = slider.transform.rotated(by: CGFloat(0.5 * Float.pi))
// Use 1.5 to invert the shadow of slider if you want
In case you work with auto layouts:
In your viewDidLoad, try:
UIView *superView = self.sizeSlider.superview;
[self.sizeSlider removeFromSuperview];
[self.sizeSlider removeConstraints:self.view.constraints];
self.sizeSlider.translatesAutoresizingMaskIntoConstraints = YES;
self.sizeSlider.transform = CGAffineTransformMakeRotation(M_PI_2);
[superView addSubview:self.sizeSlider];
It does not work with constraints, so the trick is to remove the constraints for your uislider.
You might have to resize it manually by setting its frame property.
Using an IBOutlet and setter observers.
If you want the max to be at the bottom, divide pi by positive 2
#IBOutlet weak var slider: UISlider! {
didSet {
slider.transform = CGAffineTransform(rotationAngle: .pi / -2)
}
}