【问题标题】:Adding image to Navigation bar in xcode?在xcode中将图像添加到导航栏?
【发布时间】:2012-11-28 05:33:50
【问题描述】:

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

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

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

【问题讨论】:

标签: iphone xcode uinavigationbar


【解决方案1】:

看起来这是一个已接受答案的老问题。不过,我想补充一点:

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

以下屏幕截图应该让您了解它的外观:

【讨论】:

    【解决方案2】:
    @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 
    

    更新

    只需将此代码添加到您的 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; 
    

    如果您想直接在 NavigationBar 的背景中设置图像,请使用波纹管...

    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"i_launcher.png"] forBarMetrics:UIBarMetricsDefault];
    

    另见此链接how-to-add-a-customized-image-button-to-uinavigationbar-in-iphone-sdk

    希望对你有帮助……

    【讨论】:

    • @Esha 看到这个链接 how-to-add-image-in-uinavigationbar-in-iphone-app
    • 我已经在 ViewDidLoad 中添加了它。但是我收到错误:“程序中的意外@.. 等等..我应该在哪里添加它?
    • 就在你的@implementation yourViewController 类的上方,在你的类代码之外
    • @Esha 根据您的要求创建图像并将此图像添加为导航栏的背景并使用我的第二个代码设置背景图像试试这个...
    • 我得到了你的代码,因为它可以工作:@implementation UINavigationBar (CustomImage) -(void)drawRect:(CGRect)rect { CGRect currentRect = CGRectMake(0,0,100,45); UIImage *image = [UIImage imageNamed:@"ic_launcher.png"]; [图像 drawInRect:currentRect]; } @end .
    【解决方案3】:
    - (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];
       }
    

    【讨论】:

      【解决方案4】:

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

      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 添加 NavBar,那么只需将 IBOutlet 用于您的 UINavigationBar 将其连接到 xib 中

      [myNavigationBar addSubview:view];
      

      【讨论】:

      • 不,我没明白。正如我所说,我通过 xib 添加了导航栏,因此这些代码没有被识别....
      【解决方案5】:

      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
          }
      

      【讨论】:

        【解决方案6】:

        尝试设置 imageView.frame 也请交叉检查 image initialized 是否正确

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

        【讨论】:

        • 出现错误:在这一行无法分配表达式 imageView.frame.origin.x = 30.0;
        • 喜欢这个 UIImage *image = [UIImage imageNamed: @"i_launcher.png"]; UIImageView *imageView = [[UIImageView alloc] initWithImage: image]; imageView.frame =CGRectMake(0,0,50,50);// 你必须为原点找到合适的值 self.navigationItem.titleView = imageView; [imageView 发布];还是无法添加图片?
        【解决方案7】:

        您的代码看起来不错。但是不知道为什么需要在xib中添加导航栏和item。如果您创建自己的从 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;
        }
        

        我刚刚做了如下测试:

        1. 创建一个新的基于单视图的项目。
        2. 无需任何修改即可运行。它将显示一个空白屏幕。
        3. 如上在 application.m 文件中修改 didFinishLaunchingWithOptions()。
        4. 在 viewcontroller.m 中,添加以下行。

          -(void)viewDidLoad { [超级视图DidLoad]; UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)]; view.image = [UIImage imageNamed:@"info.png"]; self.navigationItem.titleView = 视图; }

        5. 现在再次运行。您将看到一个以图片为标题的导航栏。

        【讨论】:

        • 但我没有看到在运行基于视图的应用程序上自动添加的导航栏,除非我在外部添加它。所以我已经从外部添加到我的视图控制器
        • 我已在答案中添加了代码。我刚刚测试了这些代码,它可以工作。
        • 我添加了你的代码,然后在视图控制器中添加了我的代码。但它仍然没有向我显示任何导航栏
        • 您是否从 xib 中删除了自己添加的导航栏?
        • 是的,我删除了它。它是一个基于视图的应用程序。我在 Appdelegate.m 中添加了你的代码
        【解决方案8】:

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

        将此添加到您的头文件中:

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

        【讨论】:

          【解决方案9】:

          我希望这会对您有所帮助。如果不起作用,请检查您的图像名称是否正确。

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

          【讨论】:

            猜你喜欢
            • 2011-09-05
            • 2010-12-05
            • 2012-08-15
            • 1970-01-01
            • 2018-06-23
            • 2012-03-19
            • 2015-03-09
            相关资源
            最近更新 更多