如何在UINavigationBar中创建多行backBarButtonItem

时间:2013-06-11 07:20:50

标签: iphone ios uinavigationbar

我想创建多行BackBarButtonItem。现在我的后退按钮显示如下,

enter image description here

但我想像这样显示它,颜色无关紧要,

enter image description here

我该怎么做?这是我的默认后退按钮,当我通过我的parentviewcontroller推送我的childviewcontroller,所以我不想删除它。

3 个答案:

答案 0 :(得分:0)

在这种情况下,您应该使用自定义按钮并在此按钮上使用背景图像。因为我认为可能无法实现。请参阅自定义按钮

    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(10.0f,6.5f,60.0f,30.0f)];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"accountSummary"] forState:UIControlStateNormal];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"accountSummary"] forState:UIControlStateHighlighted];
    [backBtn addTarget:self action:@selector(accountSummaryBtnPressed:) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    [self.navigationItem setLeftBarButtonItem:leftButton];


- (void)accountSummaryBtnPressed:(id)sender
{
    //--------- do stuff for account summary 
}

答案 1 :(得分:0)

我相信您可以使用ImageView和透明标签创建带有numberOflines两个以上或任何您想要限制的标签的CustomView,然后将导航后退按钮替换为使用CustomVIew启动的那个Barbutton,可以解决您的问题问题,希望完全

答案 2 :(得分:0)

你可以使用自定义图像,UILabel(MSLabel),UIButton。

MSLabel用于改变两行UILabel之间的空间。因为UILabel没有提供任何属性来做到这一点。

在项目中添加MSLabel后,请使用此代码。 backbtn.png的推荐尺寸为48 * 30.

#import "MSLabel.h"

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Set the custom back button
    UIImage *buttonImage = [[UIImage imageNamed:@"backbtn.png"]
                            resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 5)];



    //create the button and assign the image
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];

    MSLabel *lbl = [[MSLabel alloc] initWithFrame:CGRectMake(12, 4, 65, 28)];
    lbl.text = @"Account Summary";
    lbl.backgroundColor = [UIColor clearColor];
    //lbl.font = [UIFont fontWithName:@"Helvetica" size:12.0];
    lbl.numberOfLines = 0;

    lbl.lineHeight = 12;
    lbl.verticalAlignment = MSLabelVerticalAlignmentMiddle;
    lbl.font = [UIFont fontWithName:@"Helvetica" size:12];

    [button addSubview:lbl];

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

    [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;

}

-(void)back {
    NSLog(@"Dilip Back To ParentViewController");
    // Tell the controller to go back
    [self.navigationController popViewControllerAnimated:YES];
}

结果将显示如下,使用您的图片获得更好的结果。

enter image description here