自定义后退按钮不能在xcode中工作

时间:2012-08-29 14:53:50

标签: iphone ios xcode4

我正在使用这些代码进行自定义后退按钮

UIImage *buttonImage = [UIImage imageNamed:@"back-button-2.png"];

    //create the button and assign the image
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:buttonImage forState:UIControlStateNormal];

    //set the frame of the button to the size of the image (see note below)
    button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);

    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

    //create a UIBarButtonItem with the button as a custom view
    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = customBarItem;

这是后面的功能

-(void)back {   
    // Tell the controller to go back
    [self.navigationController popViewControllerAnimated:YES];
}

自定义后退按钮即将推出,但它无效。

5 个答案:

答案 0 :(得分:1)

self.navigationItem.leftBarButtonItem = [self getBackBtn];



- (UIBarButtonItem *) getBackBtn
{
    UIButton * backBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setFrame:CGRectMake(0.0f,0.0f,73.0f,33.0f)];
    [backBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"back.png"]] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    return backBarButton;    
}

//Action For LeftBarButton - BackButton
- (void)backBtnPressed
{
    [self.navigationController popViewControllerAnimated:YES];
}

答案 1 :(得分:0)

尝试将目标添加到customBarItem而不是button自定义视图

答案 2 :(得分:0)

您可以以不同的方式执行自定义UIBarButtonItem,并使其保持箭头形状

假设你的第一个控制器是A,你在A控制器的viewWillAppear中推入B:你可以做到

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"whatever.png"] style:UIBarButtonItemStyleBordered target:nil action:nil];

这将使B的后退按钮成为正常的后退按钮,但具有您选择的图像

如果您想要自定义文字,可以执行

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"title" style:UIBarButtonItemStyleBordered target:nil action:nil];

答案 3 :(得分:0)

因为UIBarButtonBarItem执行事件处理本身,而它永远不会将事件传递到其自定义后退按钮所在的UIView。在此行中,您已将其添加为子视图。

UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];

它显示但它永远不会从UIBarButtonItem得到任何事件的反馈,这只是一个视图,父UIBarButtonItem将不会传递任何事件处理一个简单的UIView。这不是合乎逻辑的行为,因此这完全没问题。


您必须将选择器UIBarButtonItem添加为

[customBarItem setAction:@selector(back)];

不适合你UIButton对象。


...或者你可以添加你的后退按钮

[self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(back)]];

您可以使用UIBarButtonItem类的任何其他init方法对其进行自定义,您可以在 UIBarButtonItem Class Reference 中找到它们。

答案 4 :(得分:-2)

试试这里: http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5

这就是我使用的......基本上你将这个方法添加到你的app委托:

- (void)customizeAppearance
{

    UIImage *buttonBack30 = [[UIImage imageNamed:@"button_back_textured_30"] 
        resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 5)];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack30 
        forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}