【问题标题】:Custom logo and button not appear in UINavigationBarUINavigationBar 中不出现自定义徽标和按钮
【发布时间】:2013-03-21 08:04:09
【问题描述】:

我有 UINavigarionController,我想在导航栏中添加自定义背景和徽标以及左按钮我可以添加背景但我的徽标和按钮没有出现,请您帮助我,提前致谢!

【问题讨论】:

    标签: ios objective-c uinavigationcontroller uinavigationbar uinavigationitem


    【解决方案1】:

    您的代码有效,我得到了徽标和按钮,但只需进行一次更改

      UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
     [button setImage:[UIImage imageNamed:@"menu.png"] forState:UIControlStateNormal];
      //[button addTarget:self action:@selector(parametres:) forControlEvents:UIControlEventTouchUpInside];
    [button setFrame:CGRectMake(0, 0, 25, 25)];
    
     UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = rightButton;
    
    UIImageView *navigationImage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 120, 30)];
    navigationImage.image=[UIImage imageNamed:@"menu.png"];
    UIImageView *workaroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
    [workaroundImageView addSubview:navigationImage];
    
    self.navigationItem.titleView= workaroundImageView;
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"menu.png"]];
    

    只需将图像类型(即不是菜单)指定为 menu.png 并通过将 UINavigationControllerDelegate 设置为您的类来将其添加到您的导航项中,如上祝您好运,请分享您的结果

    【讨论】:

    • 它不起作用,我在应用程序委托中创建它们而不是在 UIViewController 类中
    • 为什么要在 AppDeleagte 中创建通常所有此类任务都必须在视图控制器的 viewWillAppear 方法中完成
    • 因为我想在我的所有导航控制器中都拥有相同的结构,所以不要在我的所有视图中复制和粘贴所有代码
    • 据我所知,你不可能像 Shaggy 所说的那样将这段代码放在 rootViewController 中
    【解决方案2】:

    barButton 不能设置为在整个应用中使用。

    每个ViewController 都为navigationItem 提供了自己的barButtonItemstitleView,因此您必须将设置徽标和按钮的代码移至rootViewController 类

    【讨论】:

      【解决方案3】:

      你不能在你的 AppDelegate 中创建

      打开你的 ViewController.m

      - (void)viewDidLoad
       {
          self.navigationItem.title=@"Title"; //Set the title
          [self CustomButton];
          [super viewDidLoad];
      // Do any additional setup after loading the view from its nib.
      }
      
      -(void) CustomButton
      {
          UIButton *but=[UIButton buttonWithType:UIButtonTypeCustom];//create a button
          but.frame=CGRectMake(290, 4, 40, 40);// set the button frame size
          [but setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
          [but addTarget:self action:@selector(call here) forControlEvents:UIControlEventTouchUpInside];
          UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithCustomView:but];
          self.navigationItem.rightBarButtonItem = btn;
          self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
      
      }
      

      希望对你有用:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-23
        • 2013-03-18
        相关资源
        最近更新 更多