0

我正在尝试为 iOS 7 自定义应用程序的 UI。我想要 iOS 7 的 BackBarButton,但箭头和标题的颜色不同,标题的字体也不同。这是 iOS 7 后退按钮。

iOS 7 的 BackBarButton

我尝试使用以下代码对其进行自定义

    UIImage *backButtonImage = [UIImage imageNamed:@"back.png"];
    UIButton *customBackButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [customBackButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [customBackButton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
    [customBackButton setFrame:CGRectMake(0, 0, 30, 30)];       
    backButton = [[UIBarButtonItem alloc] initWithCustomView:customBackButton];
    [backButton setAction:@selector(backButtonPressed)];
    UIBarButtonItem * backButton = [[UIBarButtonItem alloc] initWithCustomView:customBackButton];

看起来像这样:

自定义 BackBarButton

上图有两个问题。

首先它没有左对齐(iOS后退按钮粘在左边)。

其次,它没有上一页的标题。

我刚刚将标题属性添加到自定义按钮。

    [customBackButton setTitle:@"title" forState:UIControlStateNormal];

但它是这样的:

自定义 backBarButton 和它的标题

我该如何解决问题(坚持左边和标题)?

4

2 回答 2

0

For this, you need to have a custom image with arrow & title. if you want to customise the title, you can just use below code>

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"My Title" style:UIBarButtonItemStylePlain target:nil action:nil];

But if you want it to set for an image, you need an image with having the back button to the left of the image.

Image: enter image description here

2x Image: enter image description here

See the images in editor, you can notice that image size is 70x32 and the arrow is more towards left end. You can have image with text also & use it.

Hope that helps.

于 2014-01-16T07:52:41.780 回答
0

我刚刚发现这是可能的

我写了以下代码,它完美地工作!

UIBarButtonItem * backButton = [UIBarButtonItem new];
UIView * backButtonView = [UIView new];
[backButtonView setFrame:CGRectMake(0, 0, 90, 32)];

UIImageView *backButtonImage = [UIImageView new];
[backButtonImage setImage:[UIImage imageNamed:@"Setting/back.png"]];
[backButtonImage setFrame:CGRectMake(-15, 0, 30, 30)];

UILabel * backButtonLabel = [UILabel new];
[backButtonLabel setFrame:CGRectMake(8, 0, backButtonView.frame.size.width, 30)];
[backButtonLabel setBackgroundColor:[UIColor clearColor]];
[backButtonLabel setTextColor:[UIColor whiteColor]];
[backButtonLabel setTextAlignment:NSTextAlignmentLeft];
[backButtonLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18]];
[backButtonLabel setText:title];

UIButton *customBackButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customBackButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[customBackButton setFrame:CGRectMake(0, 0, 90, 30)];

[backButtonView addSubview:customBackButton];
[backButtonView addSubview:backButtonImage];
[backButtonView addSubview:backButtonLabel];

backButton = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
[backButton setAction:action];
[self.navigationItem setLeftBarButtonItem:backButton animated:YES];
于 2014-01-21T07:31:14.407 回答