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;
Related
i'm making a program that has 2 Buttons in main view ;
one is called show and other one is hide,
when user presses show butoon an imageview gets added to screen
code :
-(IBAction)show{
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 155, 155)];
img.image = [UIImage imageNamed:#"icon.png"];
[self.view addSubview:img];
}
and when user presses the hide button , i want app to hide the image that has been just added (img)
but...
when i use
-(IBAction)add{
[img removeFromSuperView];
}
Xcode says "img Undecleared"
edit : Some said define the object as a public object (#property) but problem is that the imageview gets added just once. but i wanted it to add new imageview every time user presses the Show button,
so i used the [[self subviews]objectAtIndex:xx]removeFromSuperview] method to solve the problem
Set a tag for your image view & then you can get it by this tag.
[img setTag:123];
...
[[self.view viewWithTag:123] removeFromSuperview];
Create object of UIImageView in .h file like bellow..
UIImageView *img;
and in .m file viewDidLoad: method just add it like bellow..
- (void)viewDidLoad
{
///your another code
img = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 155, 155)];
img.image = [UIImage imageNamed:#"icon.png"];
img.hidden = YES;
[self.view addSubview:img];
}
and when show button press show the image
-(IBAction)show{
img.hidden = NO;
[self.view bringSubviewToFront:img];
}
and for hide just hide like bellow..
-(IBAction)add{
img.hidden = YES;
}
Make this UIImageView a member of your UIViewController class
your img object is visible in only show method not outside..thats why. Declare it outside and your problem will be solved.
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
How can I add a white background to a .png UIImage file with HSImageSidebarView?
I have tried creating a UIImageView then showing that on the sidebar, but unfortunately the app crashes with an NSException. Any ideas on how to add a white background to the image files on the sidebar if they are a UIImage instead of a UIImageView.
You will try this code add one scrollview on nib and set its contentsize on viewdidload
CGFloat x = 20;
for (int i = 0;i<[playerScores count];i++) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, 10, 60, 60)];
view.backgroundColor=[UIColor blueColor];
NSString *text = [NSString stringWithFormat:[playerScores objectAtIndex:i]];
[view setText:text];
[yardsScrollView addSubview:view];
[myLabel release];
x = x+125;
}
PlayersScore is a NSMutable array which have many text like how much view You want.
#objc-obsessive
You will need to loop through the subviews to find whether it is of kind UIImage or UIImageView by this:
id object;
if([object isKindOfClass:[UIImage Class]]) {
//Do whatever u want to here by creating a new imageView on the top of this image
//Something like this:
UIImage *image =(UIImage*)object;
UIImageView *imgView=[[uiimageview alloc]initwithimage:image];
//Now perform the background view operations on imgView i.e. instance of UIImageView.
}
//Hope it works.
How can I insert into my UIViewController an image from the resources as background image?
thanks in advance!
Add it UIViewController's view, somewhat like this:
- (void) viewDidLoad
{
UIImage *background = [UIImage imageNamed: #"background.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: background];
[self.view addSubview: imageView];
[imageView release];
[super viewDidLoad];
}
UIViewControllers don't have background images. Only views themselves have visual attributes.
UIView does not have a background image property. To display a background image, you usually simply put a UIImageView displaying the image in the view hierarchy so that it appears visually behind all other views. This can be done programmatically or in Interface Builder.
how to display an image in the navigation bar of an iPhone application? (say, right after the title)
Here's how to have the image centered in the NavBar.
UIImage *image = [UIImage imageNamed: #"NavBarImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
self.navigationItem.titleView = imageView;
[imageView release];
This code is actually contained with the Apple source for the NavBar and can be found at the iPhone Developer Center at Apple.
The source shows you various ways to manipulate both the StatusBar & NavBar.
I haven't tested this but as UINavigationBar is a view you can add subViews to it.
UIImage* myImage = [UIImage imageNamed:#"Myimage.png"];
UIImageView* myImageView = [[UIImageView alloc] initWithImage:myImage];
myImageView.frame.origin.x = 30.0; // You will have to find suitable values for the origin
myImageView.frame.origin.y = 5.0;
[myTabbar addSubView:myImageView];
[myImageView release];
You can use things like the backItem property to calculate the position of your image view.
If you want the image at the right of the nav bar, you can define it as a custom button with no action when presed, like this
UIButton* fakeButton = (UIButton *) [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourimage.png"]];
UIBarButtonItem *fakeButtonItem = [[UIBarButtonItem alloc] initWithCustomView:fakeButton];
self.navigationItem.rightBarButtonItem = fakeButtonItem;
[fakeButtonItem release];
[fakeButton release];
Simply Place that code in - (void)viewWillAppear:(BOOL)animated; so it'll work fine
and add one image having size 320x40 named Top Bar
UIImage *image = [UIImage imageNamed: #"TopBar.png"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
the navigation bar has a property called title view - set this to the image you like. Since the titleView overwrites the title of the nav bar you have to include the desired title in the image file. Still set the title to what you want so it appears on the back button when you push a view Controller
I encountered the same problem.Found out the best solution
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"background_image.png"] forBarMetrics:UIBarMetricsDefault];
Hope this would help....
Just write your own navigation bar. Therefore you have to disable the Navigation Bar fist:
Disable the top bar in the interface builder by selecting your Navigation Controller in
your Storyboard: Attributes Inspector -> Simulated Metrics -> Top Bar: select None
Afterwards you can add any HeaderView you like...
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, sFrame.size.width, 100)];
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"header_image.png"]];
self.headerView.backgroundColor = background;
// ...add buttons and labels
[self.view addSubview:headerView];