在IOS中自定义后退按钮

时间:2013-02-08 18:50:41

标签: ios cocoa-touch

如何使用barmetrics在iOS中创建没有Text的自定义后退按钮?

我想做http://a397.phobos.apple.com/us/r1000/081/Purple/v4/e6/be/2d/e6be2d9e-dc95-7e44-b1ed-9386fa9f4d02/mzl.zwjkpepo.320x480-75.jpg

之类的东西

3 个答案:

答案 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];

感谢Best Coder