Ios7 如何为BackBarButton的左栏按钮创建自定义UIBarButton
我正在尝试为iOS 7定制应用程序的UI。我想要的正是iOS 7的BackBarButton,但箭头和标题的颜色不同,标题的字体也不同。这是iOS 7后退按钮 我试着用下面的代码来定制它Ios7 如何为BackBarButton的左栏按钮创建自定义UIBarButton,ios7,customization,backbarbuttonitem,Ios7,Customization,Backbarbuttonitem,我正在尝试为iOS 7定制应用程序的UI。我想要的正是iOS 7的BackBarButton,但箭头和标题的颜色不同,标题的字体也不同。这是iOS 7后退按钮 我试着用下面的代码来定制它 UIImage *backButtonImage = [UIImage imageNamed:@"back.png"]; UIButton *customBackButton = [UIButton buttonWithType:UIButtonTypeCustom]; [custom
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];
看起来是这样的:
上图有两个问题
首先,它没有向左对齐(iOS后退按钮粘贴在左侧)
第二,它没有上一页的标题
我刚刚在自定义按钮中添加了标题属性
[customBackButton setTitle:@"title" forState:UIControlStateNormal];
但情况是这样的:
如何解决这些问题(坚持左边和标题)?为此,您需要一个带有箭头和标题的自定义图像。 如果您想自定义标题,只需使用以下代码>
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"My Title" style:UIBarButtonItemStylePlain target:nil action:nil];
但是如果你想为一个图像设置,你需要一个在图像左边有后退按钮的图像
图片:
2x图像:
在编辑器中查看图像,您可以注意到图像大小为70x32,箭头更靠近左端。您还可以使用带有文本的图像&使用它
希望有帮助 我刚刚发现这是怎么可能的 我写了下面的代码,它完美地工作了
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];