How to set specific size to UIImageView - ios

I got a weird bug going on. I'm trying to programatically initialize an UIImageView with a specific frame and image. My image is 60x60, but I'm trying to resize it to 30x30 on the screen, which seems impossible.
The following works, but show the image in 60x60:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
// imgView.frame = CGRectMake(100, 200, 30, 30);
imgView.center = CGPointMake(pointTouch.x, pointTouch.y);
[self.view addSubview:imgView];
And the following just doesn't show anything at all on the screen:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
imgView.frame = CGRectMake(100, 200, 30, 30);
imgView.center = CGPointMake(pointTouch.x, pointTouch.y);
[self.view addSubview:imgView];
What's wrong?
EDIT: for those having the same problem, here is the working code:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
imgView.frame = CGRectMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2, 30, 30);
imgView.center = CGPointMake(pointTouch.x, pointTouch.y);
[imgView setContentMode:UIViewContentModeScaleAspectFill];
[self.view addSubview:imgView];

Start experiment with
imgView.center = CGPointMake(view.bounds.x/2, view.bounds.y/2);
imgView.bounds = view.bounds;
and see if this fills the superview. Then try CGPointMake(pointTouch.x, pointTouch.y) and CGPointMake(30,30) respectively.
I'm guessing this behaviour has something to do with warning about frame:
If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored.

You could try initializing the UIImageView with a set frame, then setting the image after that:
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 30, 30)];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
imgView.image = [UIImage imageNamed:#"logo.png"];
imgView.center = CGPointMake(pointTouch.x, pointTouch.y);
[self.view addSubview:imgView];

Related

How to make a UIImageView a button?

I have a UIImageView like so:
UIImage *image = [UIImage imageNamed:#"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, 30, 30);
imageView.layer.cornerRadius = 5.0;
imageView.layer.masksToBounds = YES;
imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
imageView.layer.borderWidth = 0.1;
[self.view addSubview:imageView];
Couple of issues/questions:
How do I make this a clickable button?
I want to link to Settings (in my app) but do I have to create that image wheel like the settings icon that so many apps use or is it standard icon in iOS 7?
This is the icon I'm talking about:
Why doesn't my border show up?
1) If you want a button, you should use a UIButton and use [UIButton setImage: forState:]
2) Alternatively, you can add a UITapGestureRecognizer to your current UIImageView
here 2 choice u have to use your method
.h file
#import <QuartzCore/QuartzCore.h> //for radius and corner
1. UIButton
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setTitle:#"Cool title" forState:UIControlStateNormal];
[btn1 setFrame:CGRectMake(7, 7, 150, 160)];
[btn1 setImage:[UIImage imageNamed:#"test.png"] forState:UIControlStateNormal];
[btn1 addTarget:self action:#selector(selectFav) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
-(void) selectFav
{ //what ever you do like
}
2. UITap Gesture for UIImage View
UIImage *image = [UIImage imageNamed:#"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, 30, 30);
imageView.layer.cornerRadius = 5.0;
imageView.layer.masksToBounds = YES;
imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
imageView.layer.borderWidth = 0.1;
UITapGestureRecognizer *tapGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesture:)];
tapGesture1.numberOfTapsRequired = 1;
[tapGesture1 setDelegate:self];
[imgView addGestureRecognizer:tapGesture1];
[self.view addSubview:imageView];
- (void) tapGesture: (id)sender
{
//handle Tap...
}
Try:
UIImage *image = [UIImage imageNamed:#"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, 30, 30);
imageView.layer.cornerRadius = 5.0;
imageView.layer.masksToBounds = YES;
imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
imageView.layer.borderWidth = 0.1;
/* Here's the button Start */
UIButton *theButton = [[UIButton alloc]initWithFrame:CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y, imageView.frame.size.width, imageView.frame.size.height)];
theButton.backgroundColor = [UIColor clearColor];
theButton.showsTouchWhenHighlighted = YES;
[theButton addTarget:self action:#selector(yourAction) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:theButton];
/* Here's the button End */
[self.view addSubview:imageView];
Your border:
0.1 means is 1/10th of a point - try 1.0 for a nice fine line
Also set:
imageView.clipsToBounds = YES;
1:if you have image view and want to clickable
UIImage *image = [UIImage imageNamed:#"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
2: Add gesture to image view like this, but don’t forget to setUserInteractionEnabled:YES
UITapGestureRecognizer *TapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(markerMove:)];
[TapGestureRecognizer setNumberOfTapsRequired:1];
[TapGestureRecognizer setNumberOfTouchesRequired:1];
[imageView setUserInteractionEnabled:YES];
[imageView addGestureRecognizer: TapGestureRecognizer];

UILongPressGestureRecognizer on a navigationbar's view

I'm using this code to show a round user's avatar on my navigation bar:
UIView *avatView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 190.0f)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(-20, 75, 40, 40)];
imageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
imageView.image = [UIImage imageWithData:myUtente.imgData];
imageView.layer.masksToBounds = YES;
imageView.layer.cornerRadius = 20.0;
imageView.layer.borderColor = [UIColor blueColor].CGColor;
imageView.layer.borderWidth = 1.0f;
imageView.layer.rasterizationScale = [UIScreen mainScreen].scale;
imageView.layer.shouldRasterize = YES;
imageView.clipsToBounds = YES;
[avatView addSubview:imageView];
self.navBar.titleView = avatView;
Now I'd like this avatar to act like a button, with a 2 seconds long press on it, so I've added this code:
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(switchUser:)];
[longPressGesture setMinimumPressDuration:2.0f];
// same code as above
[avatView addGestureRecognizer:longPressGesture];
The corresponding action never gets called and I don't know why. Any idea?
Thanks.
Change the frame of avatView and imageView then gesture works like a charm.
ex:
UIView *avatView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 2, 40, 40)];

how do you center a custom UINavigation Bar Image

So I have an navigation bar I would like to customize and center no matter what I do I either get the image tiling like so.
Here is the code i am using to set the self.navigation.titleView in my UITableControllerView
self.navigationItem.titleView = [self titleView]; // happens in viewDidLoad
- (UIView *)titleView{
UIImage *headerImage = [UIImage imageNamed:#"header#2x"];
UIImageView *containerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, headerImage.size.width, headerImage.size.height)];
return containerView;
}
Thanks.
UIImageView *imgtitle = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 25)];
[imgtitle setContentMode:UIViewContentModeCenter];
self.navigationItem.titleView = imgtitle;
imgtitle.image = [UIImage imageNamed:#"logo.png"];

Gap is existing when customize the navigationbar with imageView

Im using the following codes to customize the navigationbar with an UIView. But why there is a gap in the left size of the navigationbar? ps. the gradient image is test.png.
Thanks in advance!
- (void)viewDidLoad
{
[super viewDidLoad];
// show image
UIImage *image = [UIImage imageNamed: #"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
[imageView setContentMode:UIViewContentModeScaleToFill];
imageView.frame = CGRectMake(0, 0, 320, 44);
// label
UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, 0, 100, 44)];
tmpTitleLabel.text = #"title";
tmpTitleLabel.textAlignment = UITextAlignmentCenter;
tmpTitleLabel.backgroundColor = [UIColor clearColor];
tmpTitleLabel.textColor = [UIColor whiteColor];
CGRect applicationFrame = CGRectMake(0, 0, 320, 44);
UIView * newView = [[UIView alloc] initWithFrame:applicationFrame];
[newView addSubview:imageView];
[newView addSubview:tmpTitleLabel];
self.navigationItem.titleView = newView;
}
Edit 1
Here is my test code and screen shot
- (void)viewDidLoad
{
[super viewDidLoad];
UIView * viewGreen = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIView * viewRed = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[viewGreen setBackgroundColor:[UIColor greenColor]];
[viewRed setBackgroundColor:[UIColor redColor]];
self.navigationItem.titleView = viewRed;
[self.view addSubview:viewGreen];
}
your code is work fine.just check the image and transparency.
To add the line at last and check again
[self.view addSubview:newView];
problem is solved by the following code
[self.navigationBar insertSubview:imageView atIndex:1];

how to place different UIImages

Depending on the result of some user input, i have to show from 0 to n number of images [lines in a chart style background] in a
view, changing on the x axis,
the problem is that i dont know before hand how many images are going to be needed to draw,
so i cannot create all the UIImageViews needed [nor is elegant coding]
here an example of what I mean
UIImageView *img = [[[UIImageView alloc] initWithFrame:CGRectMake(12, 30, 12, 200)] autorelease];
[img setImage:[UIImage imageNamed:#"line.png"]];
img.opaque = YES;
[self.view addSubview:img];
//[img release];
UIImageView *img2 = [[[UIImageView alloc] initWithFrame:CGRectMake(102, 30, 12, 200)] autorelease];
[img2 setImage:[UIImage imageNamed:#"line.png"]];
img2.opaque = YES;
[self.view addSubview:img2];
UIImageView *img3 = [[[UIImageView alloc] initWithFrame:CGRectMake(202, 30, 12, 200)] autorelease];
[img3 setImage:[UIImage imageNamed:#"line.png"]];
img3.opaque = YES;
[self.view addSubview:img3];
UIImageView *img4 = [[[UIImageView alloc] initWithFrame:CGRectMake(302, 30, 12, 200)] autorelease];
[img4 setImage:[UIImage imageNamed:#"line.png"]];
img4.opaque = YES;
[self.view addSubview:img4];
so how to accomplish this?
thanks a lot!
If in height you have height of your view, than:
for (int x=2; x<height; x+=100)
{
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(x, 30, 12, 200)];
[img setImage:[UIImage imageNamed:#"line.png"]];
img.opaque = YES;
[self.view addSubview:img];
[img release];
}

Resources