-1

我将这些代码用于自定义后退按钮

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];
}

自定义后退按钮即将出现,但它不起作用。

4

5 回答 5

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];
}
于 2012-08-29T14:57:22.953 回答
0

因为UIBarButtonBarItem它自己处理事件并且它永远不会将事件传递给它的UIView,您的自定义后退按钮所在的位置。在这一行中,您仅将其添加为子视图。

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

它显示,但它永远不会从. 这不是合乎逻辑的行为,因此这很好。UIBarButtonItemUIBarButtonItemUIView


你必须添加选择器之UIBarButtonItem类的

[customBarItem setAction:@selector(back)];

不为你UIButton反对。


...或者您可以添加后退按钮,例如

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

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

于 2012-08-29T15:26:38.593 回答
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];
于 2012-08-29T15:01:36.303 回答
0

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

于 2012-08-29T14:56:14.233 回答
-2

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

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

- (void)customizeAppearance
{

    UIImage *buttonBack30 = [[UIImage imageNamed:@"button_back_textured_30"] 
        resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 5)];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack30 
        forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}
于 2012-09-03T11:13:42.257 回答