1

我想创建多行 BackBarButtonItem。现在我的后退按钮显示是这样的,

在此处输入图像描述

但我想这样显示,颜色无所谓,

在此处输入图像描述

我怎样才能做到这一点?这是我在通过 parentviewcontroller 推动 childviewcontroller 时添加的默认后退按钮,所以我不想删除它。

4

3 回答 3

0

您可以使用自定义图像、UILabel(MSLabel)、UIButton 来实现。

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

在您的项目中添加 MSLabel 后,请使用此代码。推荐尺寸为backbtn.png48*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];
}

结果将像这样显示,使用您的图像以获得更好的结果。

在此处输入图像描述

于 2013-07-09T09:48:50.710 回答
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 
}
于 2013-06-11T07:27:21.053 回答
0

我相信你可以创建带有numberOflines两个以上的 ImageView 和透明标签的 CustomView 或任何你想限制它的东西,然后用 CustomVIew 启动的那个 Barbutton 替换导航返回按钮,这可以解决你的问题,希望完全

于 2013-06-11T07:27:55.970 回答