【问题标题】:change back button of UINavigationBarUINavigationBar 的后退按钮
【发布时间】:2012-05-01 18:25:11
【问题描述】:

我有这段代码可以更改 UINavigationBar 的后退按钮

// Set the custom back button
UIImage *buttonImage = [UIImage imageNamed:@"backag.png"];

//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"selback.png"] forState:UIControlStateHighlighted]; 
button.adjustsImageWhenDisabled = NO;


//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, 30, 30);

[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;

// Cleanup
[customBarItem release];

如果我把它放在 viewDidLoad 方法中,它就可以正常工作。但是,当我加载下一个视图时,会显示旧样式按钮。为了解决这个问题,我尝试将此代码放在下一个视图的 viewDidLoad 方法中,但是没有可见的按钮。

关于可能导致这种情况的任何想法?

谢谢!

【问题讨论】:

    标签: iphone uinavigationcontroller uinavigationbar uinavigationitem


    【解决方案1】:

    中应用相同的代码
    -(void)viewDidAppear:(BOOL)animated
    {
    
    }
    

    【讨论】:

      【解决方案2】:

      我在两个视图中都使用了 tis 代码,它运行良好...

      // 在两个视图的 viewDidLoad 方法中添加以下代码...

      UIButton *leftButton1 = [UIButton buttonWithType:UIButtonTypeCustom];    
      [leftButton1 setImage:[UIImage imageNamed:@"Viewone.png"] forState:UIControlStateNormal];    
      leftButton1.frame = CGRectMake(0, 0, 30, 30);
      [leftButton1 addTarget:self action:@selector(yourclickevent) forControlEvents:UIControlEventTouchUpInside];        
      self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton1];
      [leftButton1 release];
      
      UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];    
      [leftButton setImage:[UIImage imageNamed:@"ViewSecond.png"] forState:UIControlStateNormal];    
      leftButton.frame = CGRectMake(0, 0, 30, 30);
      [leftButton addTarget:self action:@selector(yourclickevent) forControlEvents:UIControlEventTouchUpInside];        
      self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
      [leftButton release];
      

      希望,这会帮助你......享受......

      【讨论】:

        【解决方案3】:

        您需要确保在 navigationItem 上设置了 hidesBackButton 属性:

        // Setup custom back button
        self.navigationItem.hidesBackButton = YES;
        

        【讨论】:

          猜你喜欢
          • 2023-04-01
          • 2011-01-04
          • 2011-06-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多