UIViewController with background image - iphone

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.

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;

UIStoryboard application and common background image

I have an iOS application which uses an UIStoryboard to control its flow. I would like to have all my views defined in the UIStoryboard to all share a common background. Is there a way I can do this without having to add an UIImageView control to each View?
I have tried this below but it causes my application crash with a stack overflow error:
-(void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:#"MyBackgroundImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
[self.view sendSubviewToBack:imageView];
}
Is there a better way to do this? What is the best way to handle this kind of theming in iOS applications?
Subclass UIViewController and override -viewDidLoad to create your image and set it as the background of the view. Now make the view controllers that require this background image subclasses of your custom view controller instead of UIViewController.
In your ViewDidLoad:
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
imgView.image = [UIImage imageNamed:#"image.png"];
[self.view addSubview: imgView];
[self.view sendSubviewToBack:imageView];
Should do the trick.
It turns out that the code I posted above is working perfectly. My solution was the same as Mark Adams' suggestion: Subclass UIViewController, override -viewDidLoad, create and set the imageView, and use the new subclass as my viewController.
I think I may have inadvertently set my new subclass to an incorrect control in Interface Builder which caused my initial solution not to work correctly.

How to wire custom UINavigationBar to UINavigationController

So I've been researching on how to have an image in UINavigationBar and the safest way to do it that works for iOS 4 and iOS 5 is via subclassing the UINavigationBar, so I had:
#interface CustomNavigationBar : UINavigationBar
#end
#implementation CustomNavigationBar
-(void) drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed: #"myNavBarImage"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
Now say I wanted to hook up ProfileViewController to have this custom UINavigationBar, how do I do that? Here's how I set ProfileViewController.
ProfileViewController *profile = [[ProfileViewController alloc] init];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:profile];
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
[profile release];
Any idea on how to wire this up? I've read that this has to be done via IB by changing the class of my UINavigationBar to the class that I just created. However, I don't have any UINavigationBar in my xib file, as it is created programatically via the UINavigationController.
One other issue also is that if I wanted to have a dynamic image that I can change/specify to the UINavigationBar, how do I do this? As of now it is hard wired that the UINavigationbar will have an image called myNavBarImage, but what if I wanted to set it to something else so I don't have to create 10 custom UINavigationBar, I somehow wanted to set the image name.
There is a solution similar to yours but uses categories instead of subclassing:
http://www.developers-life.com/custom-uinavigationbar-with-image-and-back-button.html
However, you have other choices that will make it easier for you such as adding an image as a subview to UINavigationBar. It is a safe way to do it,and I used it in a previous project which was approved.
My favorite way to do it is hiding the navigation bar and adding an imageView with buttons to have full control on how the navigation bar looks like (I especially use it when I need to add custom buttons as well).
Edit (Code to use for the second issue):
At viewDidLoad
UIImageView *imageview = [[UIImageView alloc] initWithImage: [UIImage imageNamed: #"image1.png"]];
[self.navigationController.navigationBar addSubview:imageview];
[self.navigationController.navigationBar sendSubviewToBack:imageview];
[imageview release];
At the method that changes the image:
-(void) changeNavigationBarImage: (NSString *) imageName{
for (UIView *view in self.navigationController.navigationBar.subviews) {
if ([view isKindOfClass:[UIImageView class]])
{
[(UIImageView *)view setImage:[UIImage imageNamed:imageName]];
break;
}
}
}
You can call this method when you want to change the image (i.e. button or timer)

Adding image to navigation bar of tab based application

I have tab base application
I want to add logo to the navigation bar I tried
(void)viewDidLoad {
[super viewDidLoad];
// add image
UIImage *image = [UIImage imageNamed:#"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
self.navigationItem.titleView = imageView;
}
but no image be displayed any suggestion to solve that
best regards
Try with setting frame of UIImageView *imageView ...
imageView.frame = CGRectMake(myX,myY,muWidth,myHeight)
Let me know if you still struggle ...
Do you have your leftBarButtonItem set? In the docs it says for titleView property of UINavigationItem: This property is ignored if leftBarButtonItem is not nil.

Background image for MoreNavigationController

I'd like to place an image behind the tableView in my UITabBarController moreNavigationController. I have tried inserting a subview like so when first setting up the TabBar:
UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background3.png"]];
[self.tabBarController.moreNavigationController.topViewController.view insertSubview:imageView atIndex:0];
But this places the image over the top, presumably because the tableView isn't there at the time. Is there a better time when I can call this in order to have it work properly, or an easier approach?
With some assistance from this question, I figured out how to do this. Basically, the viewController in the moreNavigationController is a single TableView, so adding a background image won't work. What I need to do was to create a new view, add the background image, and then add the moreNavigationController view on top of that. I did this by overriding viewDidLoad in a subclass of UITabBarController, but I expect it could be done elsewhere as well.
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationController *moreController = self.moreNavigationController;
if ([moreController.topViewController.view isKindOfClass:[UITableView class]]) {
UIView* newView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,367)];
UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background3.png"]];
imageView.opaque = NO;
imageView.alpha = 0.4;
[newView addSubview:imageView];
moreController.topViewController.view.backgroundColor = [UIColor clearColor];
moreController.topViewController.view.frame = CGRectMake(0,0,320,367);
[newView addSubview:moreController.topViewController.view];
moreController.topViewController.view = newView;
}
}
You could probably be smarter with the frame sizes, etc, but this works for me. Hopefully it helps someone else too.
Now you can acess backgroundView property from UITableView subclasses .
UIViewController *moreViewController = tabBarController.moreNavigationController.topViewController;
img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"BG_MORE+1.png"]];
//Got some crashs in initialization !! Need to check .
if ([moreViewController.view isKindOfClass:[UITableView class]]) {
UITableView *moreTableView = (UITableView*)moreViewController.view;
[moreTableView setBackgroundView:img];
}
Besides all the dotty mess here, you can use UIView's bringSubviewToFront: and sendSubviewToBack: to organize your subviews. Basically this should help, although if you have more subviews you will need to play around with it a little bit:
[self.tabBarController.moreNavigationController.topViewController.view addSubview:imageView];
[self.tabBarController.moreNavigationController.topViewController.view pushSubviewToBack:imageView];
//or [self.tabBarController.moreNavigationController.topViewController.view bringSubviewToFront:tableView];