How to remove the UIImageView From the custom UIView - ios

i create the the imageView by programatically and add that imageView to the Custom UIView.then i by click the button i want to add another imageView to the same custom UIView,here the old and new imageView also displaying,so i need to remove the first added imageView then i add the new imageView. help me to remove the imageView..
CGRect rect=CGRectMake(10, 10, 100,100);
UIImageView *imageView=[[UIImageView alloc]init];
imageView.frame=rect;
imageView.backgroundColor=[UIColor redColor];
[self.customView addSubview:imageView];//add the imageView to UIView

When you add the first imageView set its tag like
imageView.tag = 232;
Later you can ask your customView for this imageView and remove it.
Call this function.
[[customView viewWithTag:232] removeFromSuperview];

Use this code.
[[self.customView viewWithTag:1000] removeFromSuperView]
CGRect rect=CGRectMake(10, 10, 100,100);
UIImageView *imageView=[[UIImageView alloc]init];
[imageView setTag:1000];
imageView.frame=rect;
imageView.backgroundColor=[UIColor redColor];
[self.customView addSubview:imageView];

Related

How to set a small image on a uiimage view on a particular position?

I created an UIView.
I need to set a small image on a particular position.
I can do it setting the image in a small separate view.
But, I plan to do it dynamically on a Large UIView which is full screen.
Thanks.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,200)];
view.backgroundColor=[UIColor clearColor];
[self.view addSubview:view];
// add image in view
UIImageView *imageView =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,20,20)];
imageView.image=[UIImage imageNamed:#"image.png"];
[view addSubview:imageView];
That is what views are for: manipulating UI widgets.
By far, your best approach is to position a UIImageView.
You can also override the view drawing methods:
override func drawRect(rect: CGRect) {
// Curstom
}
Try this code.
UIImage *image = [UIImage imageNamed:#"imagename.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
//specify the frame of the imageView in the superview , here it will fill the superview
imageView.frame = catView.bounds;
// set custom frame as per your requirement
//or
// imageView.frame = CGRectMake(10,10,30,30);
//or
//according to superviews frame
//imageView.frame=CGRectMake(10,10,yourUIViewObject.bounds.size.width/2,yourUIViewObject.bounds.size.height/2);
// add the imageview to the superview
[yourUIViewObject addSubview:imageView];

ImageView with same size of View(full screen) - Objective C

How to do programmatically ImageView with same size of View in a way that there is no difference between devices ?
UIImageView *imgV =[[UIImageView alloc] initWithFrame:self.view.frame];
imgV.image=[UIImage imageNamed:#"draw.png"];
[self.view addSubview:imgV];
suppose draw.png is xx size then draw#2x = 2(xx), draw#x = 3(x*x)
please supply image draw.png, draw#2x.png , draw#3x.png in resource
hope it help
With code you can create a UIImageView with giving the frame of superview.
UIImageView* myImageView = [[UIImageView alloc]initWithFrame:self.view.frame];
[myImageView setImage:[UIImage imageNamed:#"yourImage.jpg"]];
[self.view addSubview:myImageView];
By Autolayout using storyboards we can do like this.
Drag an UIImageView to the view and provide these constraints and add.

How to change image of UIImageView that is inside a UIButton programmatically?

I created a UIButton with UIImageView and UILabel by the help of this question and answer by Tim and now I want change the image of the UIImageView on the button click event.
I tried [sender.imageView setImage:image] but it not working
you can:
1 - keep a reference to the UIImageView inside the UIButton on your viewController
2 - Subclass the UIButton and add the UIImageView yourself.
3 - When adding the UIImageView, add a tag into it (view.tag) and when getting the view from the sender, you can just
UIView *view = (UIView *)sender;
UIImageView *imageView = [view viewWithTag:123];
//do what you must with the imageView.
The tag can be any number.
Edit
this is how you should set the tag on the UIImageView, and Not on the UIButton. I copied this code from Tim's answer from here:
// Create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// Now load the image and create the image view
UIImage *image = [UIImage imageNamed:#"yourImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(/*frame*/)];
[imageView setImage:image];
// Create the label and set its text
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(/*frame*/)];
[label setText:#"Your title"];
// Set a tag on the UIImageView so you can get it later
imageView.tag = 123;
// Put it all together
[button addSubview:label];
[button addSubview:imageView];

How can I insert an imageView behind a UITableView in a fixed position (no scrolling)?

I am trying to insert an image behind my table view using:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"newpink120"]];
imageView.frame=CGRectMake([[UIScreen mainScreen]bounds].size.width-60, [[UIScreen mainScreen]bounds].size.height-60-self.navigationController.navigationBar.frame.size.height, 60, 60);
[self.tableView addSubview:imageView];
[self.tableView sendSubviewToBack:imageView];
This seems to work but the image scrolls with the table. Is there anyway to keep it fixed in the bottom right corner?
For creating a static tableview background, you have to do 2 things
1) set self.tableView.backgroundColor = [UIColor clearColor];
2) and then initialise the table background view with an UIImageView and set the image you want to show there.
[self.tableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.png"]] ];
Your image scrolls because it has been added in the tableview content and when you scroll the tableview, all the content scrolls.
But you can add the image in the superview of your tableView, behind it.
For example, if the tableView is in self.view you can try :
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"newpink120"]];
imageView.frame=CGRectMake([[UIScreen mainScreen]bounds].size.width-60, [[UIScreen mainScreen]bounds].size.height-60-self.navigationController.navigationBar.frame.size.height, 60, 60);
[self.view addSubview:imageView];
[self.view bringSubviewToFront:self.tableView];
(Make sure that the tableView has a clear backgroundColor.)

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