Adding an UIImageView and a Button - iphone

I have added an imageview, and i am trying to add an button over it. How to do it.
self.imgview = [UIImageView alloc]initWithImage:[UIImage imageNamed:#"f.png"]];
[self.imgview addSubView:button];
[self.view addSubView:imgView];
What i get is the f.png Image, i dont see the button that i added over it. How could i resolve this ?
I have no idea what is wrong and i have been stuck in this problem for hours. I need your help

You need to set the views' frames:
self.imgview = [UIImageView alloc]initWithImage:[UIImage imageNamed:#"f.png"]];
self.button = [[UIButton alloc] init];
[imgview setFrame:CGRectMake(100,100,100,100)];
[button setFrame:CGRectMake(100,100,100,100)];
[self.view addSubView:imgView];
[self.view addSubView:button];

self.imgview = [UIImageView alloc]initWithImage:[UIImage imageNamed:#"f.png"]];
[self.view addSubView:imgView];
[self.view addSubView:button];
Just add them both to the view, adding the button after the image so that it composes on top of the image. Assuming their frame's x,y coordinates also match up.

Try to use bringSubviewToFront method

Related

How to create dynamic Buttons over dynamic ImageViews while maintaining the uniqueness

I am using PhotoScroller project and want to create buttons over imageViews programmatically. ImageView is a subView of ScrollerView. My code snippet is shown below:
imageView = [[UIImageView alloc] initWithImage:image]; //initWithImage:image
//imageView.tag = i; // tag our images for later use when we place them in serial form
[imageView setContentMode:UIViewContentModeScaleToFill]; //though it has no impact
imageView.userInteractionEnabled = YES; //to enable touch interaction with image
[self addSubview:imageView];
UIButton *btn = [[UIButton buttonWithType:UIButtonTypeContactAdd] retain]; //Plus icon button
btn.frame = CGRectMake(215, 550, 100, 100);
[btn addTarget:self action:#selector(onClickHotSpotButton) forControlEvents:UIControlEventTouchUpInside]; //it wasnt working
[self addSubview:btn]; //Buttons are clickable but shows button on all images**
//[imageView addSubview:btn]; //Buttons are not clickable and are shown on all images
I have two choices rite now. Whether i make my button a subview of ImageView or ScrollView which is parent of ImageView. If i make button subview of Scrollview like [self addSubView:btn]; it displays at right position and is clickable but problem is that it is created on all images in the queue because i am making it a subview of parent view i.e scrollview. Else if i make it subview of child view i.e ImageView which is unique for every image, but still its shown on all images and its not clickable :/
Can any1 guide me on how to make it clickable and keeping the linkage between dynamic button and the image unique so that i have different buttons at various positions on each image in the queue.
Thanks in advance.
Regards,
wahib
You have to create an extra UIView for containing both UIImageView and UIButton
UIView* imageContainer = [[UIView alloc] initWithFrame:CGRectMake(0,0,1000,1000)] autorelease];
UIImageView* imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
[imageView setContentMode:UIViewContentModeScaleToFill]; //though it has no impact
imageView.userInteractionEnabled = YES; //to enable touch interaction with image
[imageContainer addSubview:imageView];
UIButton *btn = [[UIButton buttonWithType:UIButtonTypeContactAdd] retain]; //Plus icon button
btn.frame = CGRectMake(215, 550, 100, 100);
[btn addTarget:self action:#selector(onClickHotSpotButton) forControlEvents:UIControlEventTouchUpInside]; //it wasnt working
[imageContainer addSubview:btn];
[self addSubview:imageContainer];
Caution with memory leak. You have lot of unused retain in your code.

How to navigate into other screen while clicking a image in iPhone

I am very to new phone i want to display four images in one line like tabbar and also if the user clicks any of the image i want to show another screen in iPhone.
can any one tell me how can i do that and also if possible please tell me in which files i have to write the code to achieve this requirement.
Try This
In ViewDidLoad
CGRect myImageRect = CGRectMake(10.0f, 5.0f, 80.0f, 95.0f);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:myImageRect];
[imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
[imageView.layer setBorderWidth: 2.0];
imageView.opaque = YES; // explicitly opaque for performance
[self.view addSubview:imageView];
UIButton *newbutton1 = [[UIButton alloc] init];
[newbutton1 setFrame:myImageRect];
[newbutton1 addTarget:self action:#selector(myMethod:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:newbutton1];
//Method on Button Click
-(void)myMethod:(id)sender {
//Button is pressed
}
Make some xib file and build your UI.
You need to set other screens as IBOutlets and after click on button perform action to switch view.
You can set image background for UIButton.
Try, it isn't so big problem.
UIButton with images tutorial
Is a great tutorial for creating clickable images.
arrange 4 images manually in ur screen
eg
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
myImageView.image = [UIImage imageNamedWith:#"ur image.png"];
UIGestureRecognizer *recognizer1 = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(showtheUI:)];
[myImageView addGestureRecognizer:recognizer1];
recognizer1.delegate = self;
[recognizer1 release];
[self.view addSubView:myImageView ];
}
//this method will trigger when u tapped the 1st image
-(void)showtheUI(UIImageViee *)sender{
}
create 2nd imageView like
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(120, 10, 100, 100)]; etc
or use a for loop to create 4 image Views. set frame according to the i value
you need to create the custom table or more things to use for the creating a grid type of image
for that you need to use following things to use
TableView
Custom UITableViewCell
Array of images
HJCache for asynchronous image loading.
You can add buttons on your images and handle click events of buttons to Navigate to other screens

error in adding a event to an image in xcode

I want to add an event to an image. but if i add this means it will gives the warning: UIImage maynot respond to addtarget action for control events. how can i correct this code.
this is my code
UIImageView *image;
UIImage *image1=[UIImage imageNamed:#"left_arrow.png"];
image=[[UIImageView alloc]initWithImage:image1];
image.frame=CGRectMake(100,410,30,30);
[image1 addTarget:self action:#selector(buttonPressed2:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:image];
If you don't want to use an extra button you can add a UITapGestureRecognizer to the imageView.
Like this:
UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(buttonPressed2:)] autorelease];
[imageView addGestureRecognizer:tapGesture];
[imageView setUserInteractionEnabled:YES];
but this is more like a UIControlEventTouchDown event
You need to create a transparent custom UIButton on top of the UIImageView. The button will handle events. UIImageView can do that.
UIButton *btn = [[[UIButton alloc] initWithFrame:imageView.frame] autorelease];
btn.buttonType=UIButtonTypeCustom;
[btn addTarget:...];

UIScrollView with UITapGestureRecognizer and UIPageControl

I'm creating a horizontal scrolling tableview containing images. I have the swiping functionality working great, and am able to add the images to the scrollView as so in cellForRowAtIndexPath:
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, tv.frame.size.width, 78)];
[scrollView setContentSize:CGSizeMake(700, 78)];
UIImage *footImage = [UIImage imageNamed:#"Foot.png"];
UIImageView *footImageView = [[UIImageView alloc] initWithImage:footImage];
footImageView.frame = CGRectMake(0, 0, 80, 78);
footImageView.userInteractionEnabled = YES;
[scrollView addSubview: footImageView];
UIImage *handImage = [UIImage imageNamed:#"Hand.png"];
UIImageView *handImageView = [[UIImageView alloc] initWithImage:handImage];
handImageView.frame = CGRectMake(90, 0, 80, 78);
handImageView.userInteractionEnabled = YES;
[scrollView addSubview: handImageView];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(highlightImage)];
[scrollView addGestureRecognizer:tapGesture];
NSLog(#"tapGesture added to scrollView");
[[cell contentView] addSubview:scrollView];
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 50, tv.frame.size.width, 50)];
[pageControl setNumberOfPages:4];
[[cell contentView] addSubview:pageControl];
My tapGesture is registering with the selector, hightlightImage, and just logging that its calling this method correctly.
- (void) highlightImage
{
NSLog(#"tapping tapping");
}
What I REALLY am working for is the ability to highlight/select these images if tapped, and highlight/deselect them if tapped again. I will have another button on screen that will navigate to the next page (irrelevant here).
Am I going down the right path here? Obviously I will populate an NSArray of UIImages and populate the scrollView that way, but just spiking it out for now. If someone could give me some direction or an example of how to make each button separately selectable/de-selectable, that would be GREAT:)
Use UIButton instead of UIImageView.
It's got built-in selected functionality.
get rid of your gestures and add highlightImage as the action to every button.
make your highlightImage into highlightImage:(id)sender
sender should be a button so you can just do
[sender setSelected:YES];

Image displayed using CGRect is obscuring UIButtons

I have an image displayed using CGRect (code below). the problem is that it's obscuring UIButtons I have in the nib file.
CGRect myImageRect = CGRectMake(0.0f, 40.0f, 480.0f, 280.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:recipe.image]];
[self.view addSubview:myImage];
[myImage release];
if I make the CGRect smaller, the UIButtons are there.
I can't figure out how to fix this, any help is very appreciated.
thanks
You need to send your subview to the back:
[self.view sendSubviewToBack:myImage];