how to place different UIImages - ios

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];
}

Related

Wrong UIImageView dimensions on navigationItem

I am trying to add an image on navigation bar but the dimensions are wrong.
UIImageView* menuButton=[[UIImageView alloc]initWithFrame:CGRectMake(4, 7, 20, 20)];
menuButton.image=[UIImage imageNamed: Model.icon.MapList];
UIBarButtonItem *accountBarItem = [[UIBarButtonItem alloc] initWithCustomView:menuButton];
self.navigationItem.leftBarButtonItem = accountBarItem;
It seams that I don't have any control on the dimensions of the image, because when I change the values of initWithFrame nothing changes.
Also this problem appears only on iPhones greater than iPhone 5 model.
What am I missing?
You can just use simple button and set image for it.
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
[b setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)];
[b addTarget:self action:#selector(randomMsg) forControlEvents:UIControlEventTouchUpInside];
[b setImage:[UIImage imageNamed: Model.icon.MapList] forState:UIControlStateNormal];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:b];
self.navigationItem.leftBarButtonItem = item;
If you have wrong aspect ratio , you can use
[[b imageView] setContentMode: UIViewContentModeScaleAspectFit];
I'd recommend adding a border to your image view so you can see what's going on.
Here's what you're currently experiencing (using the imageView directly as the custom view of the bar button item):
- (void)_addImageViewToLeftButtonItemWithoutOuterView {
UIImageView *profileImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Homer.jpg"]];
[profileImageView setFrame:CGRectMake(0, 0, 40, 40)];
profileImageView.layer.cornerRadius = 20.0f;
profileImageView.layer.masksToBounds = YES;
profileImageView.clipsToBounds = YES;
profileImageView.layer.borderColor = [UIColor blackColor].CGColor;
profileImageView.layer.borderWidth = 1.0f;
UIBarButtonItem *rbi = [[UIBarButtonItem alloc] initWithCustomView:profileImageView];
self.navigationItem.leftBarButtonItem = rbi;
}
This is what it looks like without aspect fit:
And with aspect fit set:
And this is what I believe you want:
- (void)_addImageViewToLeftButtonItemWithOuterView {
UIView *profileView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
profileView.layer.borderColor = [[UIColor redColor] CGColor];
profileView.layer.borderWidth = 2.0f;
UIImageView *profileImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Homer.jpg"]];
profileImageView.contentMode = UIViewContentModeScaleAspectFit;
[profileImageView setFrame:CGRectMake(0, 0, 40, 40)];
profileImageView.layer.cornerRadius = 20.0f;
profileImageView.layer.masksToBounds = YES;
profileImageView.clipsToBounds = YES;
profileImageView.layer.borderColor = [UIColor blackColor].CGColor;
profileImageView.layer.borderWidth = 1.0f;
[profileView addSubview:profileImageView];
UIBarButtonItem *lbi = [[UIBarButtonItem alloc] initWithCustomView:profileView];
self.navigationItem.leftBarButtonItem = lbi;
}
Which results in this:
As you probably noticed, the sorta trick here is that you don't want to add the image view directly as the custom view within initWithCustomView of the UIBarButtonItem. Instead you want to use an outer view to hold the imageView, then you can adjust the frame of your subview to move it around.
Here's one more example if you wanted the image further to the right (obviously you won't want the border for the outer image view, but I've just left it there for demonstration purposes):
- (void)_addImageViewToLeftButtonItemWithOuterView {
// adjust this to 80 width (it always starts at the origin of the bar button item
UIView *profileView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
profileView.layer.borderColor = [[UIColor redColor] CGColor];
profileView.layer.borderWidth = 2.0f;
UIImageView *profileImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Homer.jpg"]];
profileImageView.contentMode = UIViewContentModeScaleAspectFit;
// and adjust this to be offset by 40 (move it to the right some)
[profileImageView setFrame:CGRectMake(40, 0, 40, 40)];
profileImageView.layer.cornerRadius = 20.0f;
profileImageView.layer.masksToBounds = YES;
profileImageView.clipsToBounds = YES;
profileImageView.layer.borderColor = [UIColor blackColor].CGColor;
profileImageView.layer.borderWidth = 1.0f;
[profileView addSubview:profileImageView];
UIBarButtonItem *lbi = [[UIBarButtonItem alloc] initWithCustomView:profileView];
self.navigationItem.leftBarButtonItem = lbi;
}

Unable to set Background only for UiImageView only

Im trying to give a background for an image view only as white. Following is my code.
dataArray = [[NSMutableArray alloc] init];
[dataArray addObject:#"Knowledge"];
[dataArray addObject:#"Client"];
float screenWidth = [UIScreen mainScreen].bounds.size.width;
float pickerWidth = screenWidth * 3 / 4;
float xPoint = screenWidth / 2 - pickerWidth / 2;
pickerView = [[UIPickerView alloc] init];
pickerView.transform = CGAffineTransformMakeScale(1, 1);
[pickerView setDataSource: self];
[pickerView setDelegate: self];
[pickerView setFrame: CGRectMake(xPoint, 200.0f, pickerWidth, 200.0f)];
pickerView.showsSelectionIndicator = YES;
[pickerView selectRow:2 inComponent:0 animated:YES];
UIImageView *imageview = [[UIImageView alloc]
initWithFrame:CGRectMake(30, 20, 300, 400)];
[imageview setImage:[UIImage imageNamed:#"logo.png"]];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
[imageview setBackgroundColor: [UIColor whiteColor]];
[self.view addSubview:imageview];
[self.view addSubview: pickerView];
Im facing two problems while doing so,
The background colour is being applied for both UIImageView and UiPickerView thought I have specified specifically as [imageview setBackgroundColor: [UIColor whiteColor]]; when it has to be only for UiImage.
The alignment changes when the phone is in landscape mode though I have given a proper coordinates. How can I sort this out?
Set pickerview's background color like,
pickerView.backgroundColor = [UIColor lightGrayColor]; //desired color
hope this will help :)
Try this code:
imageView.backgroundColor = [UIColor clearColor];
imageView.opaque = NO;
Try this :-
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background"]];
[self.view addSubview:backgroundView];

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];

How to set specific size to UIImageView

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];

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];

Resources