【问题标题】:UINavigationItem rightbarbuttonitem not displayingUINavigationItem rightbarbuttonitem 不显示
【发布时间】:2015-12-15 09:02:59
【问题描述】:

作为我的标题,我浏览了几个网站,都没有达到结果。但是,我成功地在导航栏上添加了一张图片。这是我目前面临的两个问题,都在 UINavigationBar 部分。 1.无法显示rightbarbutton 2. 无法隐藏后退箭头“

MasterViewController.m

//Back Button
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:@"sidemenu.png"];
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
backBtn.frame = CGRectMake(0, 0, 54, 30);

//Rightbarbutton
UIButton *infoButton = (UIButton *) [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_info.png"]];
UIBarButtonItem *infoButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
self.navigationItem.rightBarButtonItem = infoButtonItem;

//Image (Working and displaying)
UIImage *image = [UIImage imageNamed: @"icon_image.png"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(245, 0, 30, 42)];
[imageView setImage:image];
[self.navigationController.navigationBar addSubview:imageView];

【问题讨论】:

    标签: objective-c uinavigationbar uinavigationitem


    【解决方案1】:

    我认为您应该将 rightItem 的内容初始化为 UIButton,而不是从 ImageView 转换为 UIButton...所以您对后面的项目执行相同的过程..

    【讨论】:

      【解决方案2】:

      这是你的 UINavigationItem+AtAdditions.h 文件

      #import <UIKit/UIKit.h>
      
      typedef NS_ENUM(NSInteger, ATNavigationItemButtonSide) {
          ATNavigationItemButtonSideRight,
          ATNavigationItemButtonSideLeft
      };
      
      @interface UINavigationItem(ATAdditions)
      
      // This method adds an image-only button to the requested size (right or left) of the current navigation item, the image can't be tapped
      - (void)addColorfulImage:(NSString*)imageName toSide:(ATNavigationItemButtonSide)side;
      
      @end
      

      这是你的 UINavigationItem+AtAdditions.m 文件

      #import "UINavigationItem+ATAdditions.h"
      
      @implementation UINavigationItem(ATAdditions)
      
      - (void)addColorfulImage:(NSString*)imageName toSide:(ATNavigationItemButtonSide)side {
          // Create a button with the required image on it
          UIImage *image = [UIImage imageNamed:imageName];
          UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
          button.bounds = CGRectMake(0, 0, image.size.width, image.size.height );
          [button setImage:image forState:UIControlStateNormal];
      
          // Stop the events (it's an image only)
          button.userInteractionEnabled = NO;
      
          // Add the button to the navigation item
          if (side == ATNavigationItemButtonSideLeft) {
              self.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
          }
          else if (side == ATNavigationItemButtonSideRight) {
              self.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
          }
      }
      
      @end
      

      稍后您可以导入 .h 文件并将其用于视图控制器中的导航项。

      【讨论】:

      • 那是bringSubviewToFront 吗?正如我刚刚得到的“'UINavigationBar' 没有可见的@interface 声明选择器'bringSubviewToBack:”。
      • 好吧,试试我添加的代码,我之前构建了这个,但我想它可以工作
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多