How to reset the background of a UIView - iphone

After adding a background (image) to my UIView (View_0) I wish to implement a button to remove the same background.
How can I do it? I have tried to set a different image as the background of View_0 but this does not work (I suspect this background is set behind "image").
I don't want to use
[View_0 removeFromSuperview];
as I need View_0 to still be there for other purposes....
//Create an ImageView
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 940, 422)];;
//Add the ImageView as the background of the View_0
[View_0 addSubview:image];
//Move custom image behind View
[View_0 sendSubviewToBack: image];
//Failed attempt to remove the background....
View_0.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"defaultImage.PNG"]];

You have added a UIImageView to the View and Not the background of the view. So what is happening is that when u implement
View_0.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"defaultImage.PNG"]];
The background of the View is being set but the UIImageView is now on top of the View so that is why the background of the View is not being shown.
Instead if you simply want to add the background you can change it to the Action of the UIButton.
Also if you use [View_0 removeFromSuperview]; you are trying to remove the view which is not right.

//Create an ImageView
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 940, 422)];;
image.tag = 1234;
//Add the ImageView as the background of the View_0
[View_0 addSubview:image];
//Move custom image behind View
[View_0 sendSubviewToBack: image];
UIImageView *imgView = [self.view viewWithTag:1234];
[img setImage:nil];
This should work i think

Related

Changing view elements on action

I have been pondering for a while how to change the items on a UIView when you hit a button. What I would like to do is have a UITextView with text on it, then hit a button and have a UIImage appear on the screen. What I don't know how to do is how to put them together on the same view. Any help would be greatly appreciated!
To clear the content of a UIView you need to do:
for (UIView *view in [self.view subviews]) {
[view removeFromSuperview];
}
Than what you will need to do is just add the new UIImage to an UIImageView and than do:
[view addSubView:myImageView];
it should look like this:
UIImage *myImage = [UIImage imageNamed:#"image name"]; // This is the image (UIImage)
UIImageView *myImageView= [[UIImageView alloc] initWithImage:myImage]; // Insert the image to an UIImageView
[self.view addSubView:myImageView]; // Add the UIImageView to the view
running all of this code will:
clear the view
insert the image to the view
1.Place an IBoutlet uiimageview on textview
2.at viewdidload set its property hidden YES
3.On button click method set its hidden property to NO
UIImage *myImage = [UIImage imageNamed:#"image name"];
IBOutlet UIImageView *myImageView= [[UIImageView alloc] initWithImage:myImage];
connect this to imageview on uitextview where you want to fix that image
In button method add this line
myImageView.hidden = NO;

How to set UIImageView frame in ViewController for Universal App?

For a Universal App, how do I set the UIImageView frame in the ViewController?
UIView *baseView = [[UIView alloc] init];
[self.view addSubview:baseView];
// Displays UIImageView
UIImageView *myImage = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"tool.png"]];
[baseView addSubview:myImage];
When I run app it shows black screen. So i guess I have to set a frame for it.
You kinda answered your own question - yes you have to set a frame for it.
You may want to try:
UIView *baseView = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:baseView];
// Displays UIImageView
UIImageView *myImage = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"tool.png"]];
myImage.frame = baseView.bounds;
[baseView addSubview:myImage];
You could also set the autoresizingmask for baseView and myImage so that as self.view's frame changes, so do these two views.
Also, just in case you aren't using ARC - don't forget to autorelease these views before leaving this method.

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

How to add an image subview to a tableview programmatically

I'm new to programming, and I know how to add a static image behind a tableView in a .xib file, but not in code. I have tried the following code:
UIImage * image = [UIImage imageNamed:#"pic.png"];
[self.view addSubView: image];
but nothing happens when I write this!
To display an UIImage you have to use an UIImageView and put it under the UITableView using the same frame. Don't forget to set the table view's background to transparent.
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pic.png"]];
iv.frame = CGRectMake(0.0, 0.0, 300.0, 600.0);
UITableView *tv = [[UITableView alloc] initWithFrame:iv.frame style:UITableViewStylePlain];
tv.opaque = NO;
tv.backgroundColor = [UIColor clearColor];
[self.view addSubView: iv];
[self.view addSubView: tv];
[iv release];
[tv release];
You need to use a UIImageView, not a UIImage. Also, when adding a view programmatically, don't forget to set the frame.

iphone - problem with background image of uitextview

I'm trying to add a back ground image to UITextView. The image is just a small border which will be placed at the top of text view. (the image resembles teared paper image). I'm using following code
UIImageView *imgView = [[UIImageView alloc]initWithFrame: CGRectMake(0, 0, 320, 13)];
imgView.image = [UIImage imageNamed: #"teared_paper.png"];
[tView addSubview: imgView];
[tView sendSubviewToBack: imgView];
[imgView release];
My text view's height is 150 pixels only. (text view occupies only small portion of view and it is at the top of the view so that it will appear to the user when keyboard is there)
The problem is that, when I add more lines of text, text view is scrolling automatically. And at the same time, the background image that I added is also scrolling. How can I prevent the background image to stay on the top all the time irrespective of scrolling.
Don't add the image view as a subview of the text view. Instead, have both the image view and the text view as children of the main view, position the image view behind the text view, and set the background color of the text view to transparent with:
[tView setBackgroundColor:[UIColor clearColor]];
Can you try putting a UIImageView behind the UITextView and making the UITextView transparent?
Try it with:
UIImageView *imgView = [[UIImageView alloc]initWithFrame: CGRectMake(0, 0, 320, 13)];
imgView.image = [UIImage imageNamed: #"teared_paper.png"];
[tView addSubview: imgView];
[imgView release];
Just make textView transparent :
[textView setBackgroundColor:[UIColor clearColor]];
no need to bring the subview to front.
Hope this works for u..