简体   繁体   中英

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: 故事板截图描绘了navigationBar.titleView内空视图内的imageView

@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:

  1. Create a new single-view based project.
  2. Run it without any modification. It will show an empty screen.
  3. Modify didFinishLaunchingWithOptions() as above in the application.m file.
  4. 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; }

  5. Now run again. You will see a nav bar with a image as title.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM