如何使用barmetrics在iOS中创建没有Text的自定义后退按钮?
之类的东西答案 0 :(得分:7)
[[UIBarButtonItem appearance]
setBackButtonBackgroundImage:[UIImage imageNamed:@"back_button.png"]
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
您可以将其放置在您的应用委托中,并将背景图像设置为应用中的所有后退按钮(当然,对于该控件状态和条形指标)。
修改强> 如果你想要不同的东西,那么使用这个代码:
- (void)setBackButton
{
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:[UIImage imageNamed:@"BackButton.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(backButtonTapped:) forControlEvents:UIControlEventTouchUpInside];[button setFrame:CGRectMake(0, 0, 32, 32)];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:backButton] autorelease];
}
- (void)backButtonTapped:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
答案 1 :(得分:0)
这样的事情怎么样?
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(YourFrame)];
[backButton setTitle:@"<" forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(callSelectorMethod) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:backButton] autorelease];
[backButton release],backButton=nil;
答案 2 :(得分:0)
以下是解决方案:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"BackButton.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(popBack) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 32, 32)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];