ios - 在导航栏上添加自定义后退按钮

标签 ios iphone uinavigationbar

我正在尝试在导航栏上添加自定义后退按钮,但它不起作用,下面是我的代码

-(void)addLeftButton
{
    UIImage *buttonImage = [UIImage imageNamed:@"btn_back.png"];

    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];

    [aButton setBackgroundImage:buttonImage forState:UIControlStateNormal];

    aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width,   buttonImage.size.height);

    UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];

   [aButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];

   self.navigationItem.backBarButtonItem = aBarButtonItem;
}  

请告诉我这段代码有什么问题

最佳答案

试试这个。

-(void)addLeftButton
{
     UIImage *buttonImage = [UIImage imageNamed:@"btn_back.png"];

     UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];

     [aButton setBackgroundImage:buttonImage forState:UIControlStateNormal];

     aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width,   buttonImage.size.height);

     UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];

     [aButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];

     [self.navigationItem setLeftBarButtonItem:aBarButtonItem];
}

关于ios - 在导航栏上添加自定义后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19721097/

相关文章:

ios - iOS 上的 Facebook 登录会打开错误的应用程序

objective-c - AudioKit 滚动波形图未显示在物理设备上

ios - 导航栏样式

ios - iOS 8 中的 UISearchBar - UI 位移

iphone - self.navigationController.navigationBar.hidden=true; VS [self.navigationController.navigationBar setHidden :YES];

ios - 使用 AlamoFire 在 Swift 中通过证书固定实现的 HTTPS 请求失败

ios - 使用页面 View Controller 快速创建向导

ios - Clang(LLVM) 使用框架编译

iphone - RESTKit 是 ASIHTTPRequest 的良好替代品吗?

iphone - 设置单元格文本颜色