Size of Image on UINavigationBar - iphone

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];

Related

how to set a imageview at center dynamically on tableview cell

I'm wondering how to set an imageview to the center of a tableview cell dynamically.
Here is my current code.
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100,100, 20, 20)];
imgView.image = [UIImage imageNamed:#"friendsCelluserpic.png"];
cell.imageView.image = imgView.image;
Try this code :
This will align your image horizontally centre in TableViewCell
float imgwidth=40;
float imgheight=40;
imgView = [[[UIImageView alloc] initWithFrame:CGRectMake((cell.contentView.bounds.size.width/2)-(imgwidth/2),15, imgwidth, imgheight)] autorelease];
[cell.contentView addSubview:imgView];
try this, :)
UIImageView * imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"friendsCelluserpic.png"]];
imgView.frame=CGRectMake(100, 100, 20, 20);
[cell.contentView addSubview:imgView];
return cell;
you can code like this [cell.contentView addSubview:imgView];

Adding image to Navigation bar in xcode?

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];

Can't set full image in Navigation bar

I can't set full image in navigation bar (or navigation controller ,in the top of app). there will be little space remaining in the left.And when I put info button it's not on the image. How can I fix it
here is my code,pls take a look
UIImageView* img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"headerBG.png"]];
img.frame = CGRectMake(0, 0, 320, 44);
self.navigationItem.titleView =img;
[img release];
UIButton * infoDarkButtonType = [[UIButton buttonWithType:UIButtonTypeInfoLight] retain];
infoDarkButtonType.frame = CGRectMake(0.0, 0.0, 25.0, 25.0);
infoDarkButtonType.backgroundColor = [UIColor clearColor];
[infoDarkButtonType addTarget:self action:#selector(showInfo) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithCustomView:infoDarkButtonType];
self.navigationItem.leftBarButtonItem = infoButton;
[infoDarkButtonType release];
[infoButton release];
very thank you.
Me.
If you want the navigation bar to show an image cant you try this
#implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed: #"custom_nav_bar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
Use this code line ot add image in NavigationController...
UIImageView* img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"headerBG.png"]];
img.frame = CGRectMake(0, 0, 320, 44);
[self.navigationController.navigationItem addSubView:img];
[img release];
now, you set image in Navigation..

Add title and image to navigationbar

I need so set the name of the UIViewController and an image to the navigationBar. So far I am able to display the image, but the title is of course missing.
// show image
UIImage *image = [UIImage imageNamed: #"bar_icon.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
imageView.frame = CGRectMake(0, 0, 40, 40);
self.navigationItem.titleView = imageView;
[imageView release];
Thanks for your help,
BR, doonot
// show image
UIImage *image = [UIImage imageNamed: #"bar_icon.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
imageView.frame = CGRectMake(70, 0, 40, 40);
// label
UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 0, 100, 40)];
tmpTitleLabel.text = #"Mannschaft";
tmpTitleLabel.backgroundColor = [UIColor clearColor];
tmpTitleLabel.textColor = [UIColor whiteColor];
CGRect applicationFrame = CGRectMake(0, 0, 300, 40);
UIView * newView = [[[UIView alloc] initWithFrame:applicationFrame] autorelease];
[newView addSubview:imageView];
[newView addSubview:tmpTitleLabel];
self.navigationItem.titleView = newView;
[imageView release];
[tmpTitleLabel release];
Create a UIView with 2 subview: a UIImageView and a UILabel. Set the titleView to the UIView.

Change background image of searchbar

It's easy to change background color of a searchbar.But, I have no idea how to change its background image.There is no direct method.
I need help...............
I appreciate any given idea...
UISearchBar* bar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIImageView* iview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"test.png"]];
iview.frame = CGRectMake(0, 0, 320, 44);
[bar insertSubview:iview atIndex:1];
[iview release];
[self.view addSubview:bar];
[bar release];
searchBar.backgroundImage = [UIImage imageNamed:#"searchBackground"];
if you want to use an image as the background of the search bar, you can use,
searchBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"search_bg.png"]];