Adding image to Navigation bar in xcode? - iphone

I have added Navigation bar and Navigation Item in xib. Now I need to add image to the left of Navigation bar, but its not getting added.
This is the code I'm working on:
- (void)viewDidLoad
{
UIImage *image = [UIImage imageNamed: #"i_launcher.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
self.navigationItem.titleView = imageView;
[imageView release];
}
I can't understand why the image is not getting added. How can I add it?

Looks like this is an old question with an accepted answer. However, I wanted to add:
In at least Xcode 5, if you are using storyboards then you can add the image as the navigationBar's title by dragging an empty view into the title area, and then placing the ImageView inside the empty view.
The following screenshot should give you an idea of how it looks:

#implementation UINavigationBar (CustomImage) -(void)drawRect:(CGRect)rect {
CGRect currentRect = CGRectMake(0,0,100,45);
UIImage *image = [UIImage imageNamed:#"ic_launcher.png"];
[image drawInRect:currentRect];
}
#end
UPDATE
just add this code in your viewDidLoad: method..
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourimage.png"]];
UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourimage2.jpg"]]];
self.navigationItem.rightBarButtonItem = item;
and also if you want to direct set image in background of NavigationBar then use bellow line...
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"i_launcher.png"] forBarMetrics:UIBarMetricsDefault];
also see this Link how-to-add-a-customized-image-button-to-uinavigationbar-in-iphone-sdk
i hope this help you...

- (void)viewDidLoad
{
UIImage *image = [UIImage imageNamed:#"Your Image"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(5, 10, 72, 19);
[self.navViewController.navigationBar addSubview:imageView];
}

If you want to set your image at particular point in navigation bar then you can make a view and then add your image inside the view, adjust your image frame and then add this view to your navBar.
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"Default.png"]];
[image setFrame:CGRectMake(0, 0, 44, 44)];
[view addSubview:image];
[self.navigationController.navigationBar addSubview:view];
If you are adding NavBar from xib then just make IBOutlet for your UINavigationBar connect it in xib then
[myNavigationBar addSubview:view];

For Swift 4:
override func viewDidAppear(_ animated: Bool) {
// 1
let nav = self.navigationController?.navigationBar
// 2
nav?.barStyle = UIBarStyle.black
nav?.tintColor = UIColor.yellow
// 3
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
imageView.contentMode = .scaleAspectFit
// 4
let image = UIImage(named: "Apple_Swift_Logo")
imageView.image = image
// 5
navigationItem.titleView = imageView
}

Try to set the imageView.frame also please cross check the image initailized properly
UIImage *image = [UIImage imageNamed: #"i_launcher.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
imageView.frame.origin.x = 30.0; // You will have to find suitable values for the origin
imageView.frame.origin.y = 5.0;
self.navigationItem.titleView = imageView;
[imageView release];

Your code looks ok. But I don't know why you need to add navigation bar and item in xib. If you create your own view controller derived from UIViewController, it has contained navigation bar. You may try to remove yourself added navigation bar and item from xib and see what's happen.
If you want to show nav bar on the first view of a view-based application, you need to modify your application class code as below:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[TestViewController alloc] initWithNibName:#"TestViewController" bundle:nil];
// REMOVE THIS LINE !!!!!!!!!!!!!
//self.window.rootViewController = self.viewController;
// ADD BELOW TWO LINES !!!!!!!!!!!!!!!!!!!!!!!
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
I just did a test like the following:
Create a new single-view based project.
Run it without any modification. It will show an empty screen.
Modify didFinishLaunchingWithOptions() as above in the application.m file.
In the viewcontroller.m, add the following lines.
-(void)viewDidLoad
{
[super viewDidLoad];
UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
view.image = [UIImage imageNamed:#"info.png"];
self.navigationItem.titleView = view;
}
Now run again. You will see a nav bar with a image as title.

To hide / show the image between movements to another controller do this. Adjust the CGRectMake dimensions accordingly.
Add this to your header file:
#property (weak, nonatomic) UIImageView *navBarLogo;
Add this to your .m controller file:
- (void)viewWillAppear:(BOOL)animated {
UIImage *image = [UIImage imageNamed:#"image_name.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(53, 7, 210, 30);
self.navBarLogo = imageView;
[self.navigationController.navigationBar addSubview:imageView];
}
- (void)viewWillDisappear:(BOOL)animated {
[self.navigationController.navigationBar sendSubviewToBack:self.navBarLogo];
}

I hope this will help you sure. if not work mean check your image name is correct.
UIImage *yourimage = [UIImage imageNamed: #"i_launcher.png"];
UIButton *buttonBarButton = [ UIButton buttonWithType:UIButtonTypeCustom];
buttonBarButton.frame = CGRectMake(0, 0,yourimage.size.width,yourimage.size.height);
[buttonBarButton setBackgroundImage:yourimage forState:UIControlStateNormal];
[buttonBarButton addTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonBarButtonItem = [[[UIBarButtonItem alloc]initWithCustomView:buttonBarButton] autorelease];
self.navigationItem.leftBarButtonItem = buttonBarButtonItem;
if you wish add action means :
[buttonBarButton addTarget:self action:#selector(your function) forControlEvents:UIControlEventTouchUpInside];

Related

Why is my attempt to display an image over the background causing a white display?

I have a background coming from Default-568h#2x.png. I want to display a shrunk PNG over part of the background, but calling:
[[CJSHCardView alloc] initWithScale:.1];
turns the display white. This does not happen if I comment this line, but in the function call, even if I make it return immediately it turns the display white after half a second:
- (id)initWithScale:(float)scale
{
UIImage *img = [UIImage imageNamed:#"note.png"];
self = [super initWithImage:img];
if (self != nil)
{
self.frame = CGRectMake(0, 0, img.size.width * scale, img.size.height * scale);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:self.frame];
myImage.opaque = NO;
[self addSubview:myImage];
}
return self;
}
Moving/copying the return statement to be the first in initWithScale makes no discernible change from how it is with the return statement properly at the end: a brief view of the background, followed by a white screen. note.png is in "Supporting Files".
Do you see any gotchas where I can change things so that a shrunken version of the note (maintaining aspect ratio) displays? The note.jpg image does not have a pixel of white.
Thanks,
--EDIT--
Regarding the first comment:
#implementation CJSHCardView : UIImageView
- (id)initWithFrame:(CGRect)frame
{
NSAssert(NO, #"Call initWithHeight instead.");
return self;
}
Does that answer your question?
--SECOND EDIT--
Thank you for your response and your code, Nick. I have slightly altered it to fit ARC and to initially set a fixed-width scale, but I was a little unsure of what method to put it in. I tried the ViewController's viewDidLoad() method, but that resulted in the background being shown without hide or hair of the note. My present viewDidLoad() method reads:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *backgroundImage = [UIImage imageNamed:#"Default-568h"];
CGRect containerRect = CGRectZero;
containerRect.size = [backgroundImage size];
UIView *containerView = [[UIView alloc] initWithFrame:containerRect];
float scale=.1;
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
[containerView addSubview:backgroundView];
[backgroundView sizeToFit];
// [backgroundView release];
UIImage *noteImage = [UIImage imageNamed:#"note"];
CGSize noteSize = [noteImage size];
UIImageView *noteView = [[UIImageView alloc] initWithImage:noteImage];
[noteView setContentMode:UIViewContentModeScaleAspectFit];
[noteView setFrame:CGRectMake(0, 0, noteSize.width * scale, noteSize.height * scale)];
[containerView addSubview:noteView];
// [noteView release];
}
Thanks,
Subclassing UIImageView is not necessary. Just create a container UIView and add 2 UIImageView subviews.
Edit - this should work for your implementation:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *backgroundImage = [UIImage imageNamed:#"Default-568h"];
CGRect containerRect = CGRectZero;
containerRect.size = [backgroundImage size];
UIView *containerView = [[UIView alloc] initWithFrame:containerRect];
[[self view] addSubview:containerView];
float scale=.1;
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
[containerView addSubview:backgroundView];
[backgroundView sizeToFit];
UIImage *noteImage = [UIImage imageNamed:#"note"];
CGSize noteSize = [noteImage size];
UIImageView *noteView = [[UIImageView alloc] initWithImage:noteImage];
[noteView setContentMode:UIViewContentModeScaleAspectFit];
[noteView setFrame:CGRectMake(0, 0, noteSize.width * scale, noteSize.height * scale)];
[containerView addSubview:noteView];
}

Size of Image on UINavigationBar

I want to put Custom image on my UINavigationBar,
When i tried this two code snippets,
[self.navigationItem setTitleView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]]];
And
UIButton *logoView = [[UIButton alloc] initWithFrame:CGRectMake(0,0,70,30)];
[logoView setBackgroundImage:[UIImage imageNamed:#"logo.png"] forState:UIControlStateNormal];
[logoView setUserInteractionEnabled:NO];
self.navigationItem.titleView = logoView;
image which i can see on my Device is getting blur or it is getting streched.
I made image with Resolution of 180*40(Width*Height)
I want to know the exact size of Image. what, it would be ?
Thanks in advance.
use this
UIImageView *navigationImage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 98, 34)];
navigationImage.image=[UIImage imageNamed:#"topNav-logo.png"];
UIImageView *workaroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 98, 34)];
[workaroundImageView addSubview:navigationImage];
self.navigationItem.titleView=workaroundImageView;
hope this helps.
Create Image with size (320 x 44) and set as a Title like bellow..
self.navigationItem.titleView = yourImageView;
See whole example like bellow...
- (void)viewDidLoad{
UIImageView* imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
self.navigationItem.titleView = imgView;
[imgView release];
}
You need to first initialize the titleView. and then set it the imageView as a subView:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
self.navigationItem.titleView = [[UIView alloc] initWithFrame:imageView.bounds];
[self.navigationItem.titleView addSubview:imageView];

Navigation Controller not working properly

I am using a navigation controller in my current application, but I had an issue with the navigation controller with the iOS4 and iOS5 so i tried to write the code for both iOS 4 & 5
if([[UINavigationBar class] respondsToSelector:#selector(appearance)]) //iOS >=5.0
{
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
else
{
self.navigationController.navigationBar.layer.contents = (id)[UIImage imageNamed:#"header.png"].CGImage;
}
But problem is when I run my app on iOS 4 version my navigation Controller look like this.
please suggest me.
Thank after a long search I tried this code which helped me.
import "ImageViewController.h"
#implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:#"background.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
implement this code in your .m file
#implementation ImageViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
UIImageView *backGroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]];
[self.navigationController.navigationBar insertSubview:backGroundView atIndex:0];
[backGroundView release];
}
#end
UINavigationController *navControl;
In else part, try like this.
UINavigationBar *navBar = self.navControl.navigationBar;
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
imgView.image = [UIImage imageNamed:#"header.png"];
[navBar addSubview:imgView];
[imgView release];

Make a Custom view that has a UIImage with a UILabel inside (at bottom of image)

I try to make a cool news app like BBC news. I think each news item (see picture) must have a UIImage and UILabel (title of news) inside it --> We must create custom view.
I adready have read how to create custom view in Apple Developer website but it only introduces, not has some samples.
My question is:
+ How can I create that customview (a UIImage with a UILabel inside)
+ How can I put that view in tableview in my main screen app.
Thank in advance. Sorry for poor English.
You can accomplish this task in many ways
1.You can create a Custom View with UIImageView and UILabel and add it as subview in tableViewCell
2.You can create Custom TableViewCell with the required labels and UIImageView and use it directly.
To create Custom View with UIImageView and UILabel
Right click "project" -> Choose "New File" -> Select "Objective C Class"-> Select "Subclass of UIView"
CustomView.h
#interface CustomView : UIView
{
}
- (id)initWithFrame:(CGRect)frame withImage:(UIImage *)img withLabel:(NSString *)lbl ;
#end
CustomView.m
#implementation CustomView
- (id)initWithFrame:(CGRect)frame withImage:(UIImage *)img withLabel:(NSString *)lbl
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
UIImageView * imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height - 30)];
[imageView1 setImage:img];
[self addSubview:imageView1];
[imageView1 release];
UILabel * label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, frame.size.height - 30, frame.size.width,30)];
label1.text = lbl;
label1.textColor = [UIColor blackColor];
[self addSubview:label1];
[label1 release];
}
return self;
}
#end
To use it
Import CustomView using #import "CustomView.h" and
CustomView * cView = [[CustomView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) withImage:[UIImage imageNamed:#"img.png"] withLabel:#"Testasddddddddddddd"];
[self.view addSubview:cView];
[cView release];
Try this..
UIButton *objBtn;
objBtn = [[UIButton alloc] init];
objBtn.frame = CGRectMake(x, y, W, H);
[objBtn setBackgroundImage:[UIImage imageNamed:#"defalt_person_01.png"] forState:UIControlStateNormal];
[objBtn addTarget:self action:#selector(SelectLanguageBtn:) forControlEvents:UIControlEventTouchUpInside];
objBtn.tag = 101;
[self.view addSubview:objBtn];
//Your News On Imagebtn
UILabel *newsLab=[[UILabel alloc]initWithFrame:CGRectMake(BtnX, BtnY, W, H)];
newsLab.textAlignment=UITextAlignmentCenter;
newsLab.textColor=[UIColor orangeColor];
newsLab.backgroundColor=[UIColor clearColor];
newsLab.text = #"My News";
newsLab.font=[UIFont fontWithName:#"TimesNewRomanPS-BoldMT" size:30];
[objBtn addSubview:newsLab];
Apart from #Shaheen M Basheer answer u can also try MGBox2 open source library which helps you to create Custom UIViews and arrange them in different Layout,also checkout sample given with it.It also support Grid layout which you can use to get what u intend.

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.