UIBarButtonItem backButton设置图像没有后退文本会导致标题对齐问题

时间:2013-05-28 16:55:15

标签: ios objective-c uinavigationbar uibarbuttonitem uinavigationitem

我想在我的backButton中使用navigationController的图片。我只想要使用的图像,根本没有显示“后退”文字,所以我做了:

UIImage *backButtonImage = [[UIImage imageNamed:@"ZSSBackButton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 25, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, backButtonImage.size.height*2) forBarMetrics:UIBarMetricsDefault];

这很好用,但似乎有副作用使长navigationItem个标题看起来偏离中心,即使它真的只是留下白色空间的背景文本应该是:

enter image description here

有没有办法可以让标题更左边?

1 个答案:

答案 0 :(得分:0)

您可以使用 titleView 属性管理UINavigationbar标题的位置。

UIView *backView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];// Here you can set View width and height as per your requirement for displaying titleLbl position in navigationbar 
[backView setBackgroundColor:[UIColor clearColor]];
UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
[backView addSubview:titleLbl];
titleLbl.text  = @"YOUR TITLE";
titleLbl.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = backView;

编辑:您可以创建自定义按钮并将其指定为左按钮:

UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.frame = CGRectMake(0,0,51,31);
[btnBack setBackgroundImage:[UIImage imageNamed:@"ZSSBackButton"] forState:UIControlStateNormal];
[btnBack addTarget:self action:@selector(btnBackProfile_TouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
[self.navigationItem setLeftBarButtonItem:leftBtn];

希望它对你有所帮助。