How to add image in UINavigationBar in IPhone app

后端 未结 3 1368
無奈伤痛
無奈伤痛 2020-12-23 17:44

In my app, i want to add image(logo) in Navigation bar. I am working on XCode 4.2 and iOS 5.

I know UINavigationBar, UIToolBar has been cha

相关标签:
3条回答
  • 2020-12-23 18:19

    Another way without using a viewController

    // Create your image
    UIImage *image = [UIImage imageNamed: @"logo.png"];
    UIImageView *imageview = [[UIImageView alloc] initWithImage: image];
    
    // set the image view to the title view
    self.navigationItem.titleView = imageview;
    
    0 讨论(0)
  • 2020-12-23 18:21

    Swift version: You can create a protocol extension for using the function in your view controllers

    protocol Customizable {
        var navigationItem: UINavigationItem { get }
    }
    
    extension Customizable {
        func setNavBarLogo() {
    
            let logo = UIImage(named: "logo")
            let logoImageView = UIImageView(image: logo)
    
            self.navigationItem.titleView = logoImageView
        }
    }
    
    0 讨论(0)
  • 2020-12-23 18:25

    One way to do this is to use UINavigationItem.titleView and UINavigationItem.rightBarButtonItem. Like this :

    viewController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage.png"]];
    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage2.jpg"]]];    
    viewController.navigationItem.rightBarButtonItem = item;    
    

    Here I am using UIImageView as custom view, but it can be UIButton with custom image.

    0 讨论(0)
提交回复
热议问题