Iphone 在xcode中将图像添加到导航栏?

Iphone 在xcode中将图像添加到导航栏?,iphone,xcode,uinavigationbar,Iphone,Xcode,Uinavigationbar,我在xib中添加了导航栏和导航项。现在我需要将图像添加到导航栏的左侧,但它没有被添加。 这是我正在研究的代码: - (void)viewDidLoad { UIImage *image = [UIImage imageNamed: @"i_launcher.png"]; UIImageView *imageView = [[UIImageView alloc] initWithImage: image]; self.navigationItem.titleView = image

我在xib中添加了导航栏和导航项。现在我需要将图像添加到导航栏的左侧,但它没有被添加。 这是我正在研究的代码:

- (void)viewDidLoad
{
UIImage *image = [UIImage imageNamed: @"i_launcher.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
    self.navigationItem.titleView = imageView;
    [imageView release];
}

我不明白为什么图像没有被添加。我如何添加它?

尝试设置图像视图。框架也请交叉检查初始化的图像

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

你的代码看起来不错。但我不知道为什么需要在xib中添加导航栏和项。如果您从UIViewController创建自己的视图控制器,则它包含导航栏。您可以尝试从xib中删除自己添加的导航栏和项目,看看会发生什么

如果要在基于视图的应用程序的第一个视图上显示导航栏,需要修改应用程序类代码,如下所示:

- (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;
}
我只是做了如下测试:

  • 创建新的基于单个视图的项目
  • 不做任何修改就运行它。它将显示一个空屏幕
  • 如上所述,在application.m文件中修改didFinishLaunchingWithOptions()
  • 在viewcontroller.m中,添加以下行

    -(无效)viewDidLoad { [超级视图下载]; UIImageView*view=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,30,30)]; view.image=[UIImage ImageName:@“info.png”]; self.navigationItem.titleView=视图; }

  • 现在再跑一次。您将看到一个导航栏,标题为图像

  • 更新

    只需在
    viewDidLoad:
    方法中添加此代码

    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; 
    
    而且,若你们想在导航栏的背景中直接设置图像,那个么使用下面的行

    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"i_launcher.png"] forBarMetrics:UIBarMetricsDefault];
    
    另请参见此链接


    我希望这能帮助你…

    我希望这能帮助你。如果不工作,请检查您的图像名称是否正确

    - (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];
       }
    
    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;
    
    如果您希望添加操作,则表示:

    [buttonBarButton addTarget:self action:@selector(your function) forControlEvents:UIControlEventTouchUpInside];
    

    如果要在导航栏中的特定点设置图像,则可以创建视图,然后将图像添加到视图中,调整图像边框,然后将此视图添加到导航栏中

    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];
    
    如果您要从xib添加导航栏,则只需为您的
    UINavigationBar创建
    IBOutlet
    ,然后在xib中连接它

    [myNavigationBar addSubview:view];
    

    要在移动到另一个控制器之间隐藏/显示图像,请执行以下操作。相应地调整尺寸

    将其添加到头文件:

    @property (weak, nonatomic) UIImageView *navBarLogo;
    
    将此添加到.m控制器文件:

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

    看起来这是一个老问题,有一个公认的答案。不过,我想补充一点:

    至少在Xcode 5中,如果您使用的是故事板,则可以通过将空视图拖动到标题区域,然后将ImageView放置在空视图中,将图像添加为导航栏的标题

    下面的屏幕截图可以让您了解它的外观:
    对于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
        }
    

    检查我的答案获取错误的可能重复:表达式在此行imageView.frame.origin.x=30.0不可赋值;喜欢这个UIImage*image=[UIImage imagename:@“i_launcher.png”];UIImageView*imageView=[[UIImageView alloc]initWithImage:image];imageView.frame=CGRectMake(0,0,50,50);//您必须为origin self.navigationItem.titleView=imageView找到合适的值;[图像视图发布];仍然无法添加d图像?但我看不到导航栏在运行基于视图的应用程序时自动添加,除非我从外部添加。因此,我已将外部添加到我的视图控制器中。我已添加了相应的代码。我刚刚测试了那些代码,它可以工作。我添加了你的代码,然后在视图控制器中添加了我的代码。它仍然没有向meDid显示任何导航栏。你从xib中删除了你自己添加的导航栏吗?是的,我删除了它。它是一个基于视图的应用程序。我在Appdelegate中添加了你的代码。m@Esha查看此链接如何在iphone应用程序的uinavigationbar中添加图像我已经在ViewDidLoad中添加了它我遇到了错误:“程序中出现意外@。。等等..我应该在哪里添加它?就在@implementation yourViewController类的上面,在类之外code@Esha根据您的要求创建图像,并将此图像添加为导航栏的背景,并使用我的第二个代码设置背景图像尝试此…我得到了您的代码,因为此代码正在工作:@implementation UINavigationBar(CustomImage)-(void)drawRect:(CGRect)rect{CGRect currentRect=CGRectMake(0,0100,45);UIImage*image=[UIImage ImageName:@“ic_launcher.png”];[image drawInRect:currentRect];}@end.No我不明白。正如我说过的,我通过xib添加了导航栏,所以这些代码没有被识别。。。。
    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
        }