【问题标题】:Add back arrow in UIBarButtonItem在 UIBarButtonItem 中添加返回箭头
【发布时间】:2014-02-27 21:25:29
【问题描述】:

我有这个创建 UIBarButtonItem 的代码:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                               initWithTitle: @"ToDo"
                               style: UIBarButtonItemStyleBordered
                               target:self action: @selector(popToRoot:)];
[self.navigationItem setLeftBarButtonItem:backButton];

问题在于删除了左箭头,我想将它包含在文本旁边。我该怎么做?

谢谢,

彼得

【问题讨论】:

  • 创建后退按钮的图像并使用图像...
  • 我将如何做到这一点?
  • 我希望在后退按钮上你会有相同的文字......对吗?你会有自定义文本吗?
  • 文本将始终为“ToDo”,如上面的代码中所述:)

标签: ios objective-c uinavigationbar uibarbuttonitem uibarbuttonitemstyle


【解决方案1】:

试试这个代码

UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];  
UIImage *backBtnImage = [UIImage imageNamed:@"BackBtn.png"]  ;  
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];  
[backBtn addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];  
backBtn.frame = CGRectMake(0, 0, 54, 30);  
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;  
self.navigationItem.leftBarButtonItem = backButton;

然后像这样定义 goback 方法

- (void)goback
{
    [self.navigationController popViewControllerAnimated:YES];
}

注意:BackBtn.png 将包含 ToDo 文本和返回按钮图像...

另一种方式

如果你想不用图片,那么添加下面的代码...

-(void)viewWillAppear {
    self.title = @"Your Title"
}

-(void)viewWillDissapear {
    self.title = @"ToDo"
}

这将在您转到下一个视图时创建 ToDo...

【讨论】:

  • 当我尝试你的代码时,我什么也没得到。根本没有按钮?
  • 这听起来很愚蠢,如何添加图像?默认情况下它不应该已经存在,因为它是 iphone 应用程序的标准配置吗?
  • 它的标准...但您需要创建您正在寻找的图像...还有另一种方法,但它的丑陋方式...
  • @PeterStuart :你尝试过另一种方式吗?有效吗?
  • 返回按钮是什么?在尝试第二种方式时,请删除您在我的第一种方式中拥有的代码...甚至删除您的代码...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 2021-04-08
  • 1970-01-01
相关资源
最近更新 更多