I have successfully added my logo on my navigation bar with this code;
UIImage *image = [UIImage imageNamed: #"top-logo"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
self.navigationItem.titleView = imageView;
the problem is my logo's height is 54 pixels, where the navigation bar, as defaults has a height of 44 pixels. Logo was intentionally designed to overflow from the bottom of the navigation bar, but I have to change the bounds of navigation bar to do that and I don't want to run over Apple's Guidelines. But I need the imageView which is the titleView of the navigation item to overflow from the navigation bar.
Besides, for one of my apps I reduced the height of navigation bar, which started to act funny when app goes to background and come back (height started to change back to normal, which caused black background within the navigation bar).
Here is a post with a similar situation. The accepted answer used a UIButton instead of an imageview.
Code from accepted answer:
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *logoView = [[[UIButton alloc] initWithFrame:CGRectMake(0,0,85,40)] autorelease];
[logoView setBackgroundImage:[UIImage imageNamed:#"navBarLogo.png"] forState:UIControlStateNormal];
[logoView setUserInteractionEnabled:NO];
self.navigationItem.titleView = logoView;
}
Image instead of title in navigation bar of TTLauncherView
First, in your -viewDidLoad method, create an UIImageView that fits the NavigationBar size.
After that you put your logo inside that UIImageView and set it to be your titleView. The code is below:
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,40,40)];
image.contentMode = UIViewContentModeScaleAspectFit;
[image setImage: [UIImage imageNamed:#"logo"]];
self.navigationItem.titleView = image;
I was looking for a similar solution, but I tried something a little different and worked perfectly:
-(void)viewDidLoad
{
[super viewDidLoad];
//Set the place where you want your logo to appear on your navigation bar (just an example):
UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(130, 20, 50, 54)];
imageView.image = [UIImage imageNamed:#"yourImage#2x.png"];
[self.navigationController.view addSubview:imageView];
}
If you're just looking for a way to add an image to your navigationbar and you're using Swift, this is the code you need:
override func viewDidLoad() {
super.viewDidLoad()
let imageView = UIImageView(image: UIImage(named: "your_logo"))
navigationItem.titleView = imageView
}
Related
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
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.
I want to display the image in navigation bar instead of a back button with an action. So now I have used image view and displayed the image in the navigation bar. Now I want to write the actions for image view, when touch or click the image view. Is it possible to write an action for image view?
I am currently working in iPhone OS 4.0.
Please see my previous question.
Here is my code:
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0,5,40,40)];
[self.navigationController.navigationBar addSubview:view1];
view1.backgroundColor = [UIColor clearColor];
UIImage *img = [UIImage imageNamed:#"Back.png"];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
[view1 addSubview:imgView];
How can I write the actions for image view?
Before iOS 3.2, nou would need a sublass of UIImageView and implement the touch handlers.
Or replace your UIImageView with a UIButton with custom type.
With iOS 3.2+, UIGestureRecognizer is what gets your things done efficiently - just add the appropriate one to your view.
Don't add views directly to the navigation bar unless you're prepared for it to break in a future OS release.
UIButton * b = [UIButton buttonWithType:UIButtonTypeCustom];
b.image = [UIImage imageNamed:#"Back.png"];
b.frame = (CGRect){{0,0},{40,40}};
[b addTarget:self action:#selection(wahatever) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:b] autorelease];
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];