如何在backbuttonitem上对齐标题

时间:2012-10-11 09:38:04

标签: ios

我是新手,所以我无法上传图片,但是当我在iOS中自定义backButtonItem时我遇到了问题 我写的代码:

UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:@"backBtn.png"];
backBtn.frame = CGRectMake(0, 0, backBtnImage.size.width, backBtnImage.size.height);
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
backBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
[backBtn addTarget:self action:@selector(popBack) forControlEvents:UIControlEventTouchUpInside];
[backBtn setTitle:@"Back" forState:UIControlStateNormal];
UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];

[backBtnItem setStyle:UIBarButtonItemStyleDone];
self.navigationItem.leftBarButtonItem = backBtnItem;

但是“back”的标题在backButton上没有对齐。 我怎么能对齐后台按钮的标题?

1 个答案:

答案 0 :(得分:0)

试试这个:

backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;