0

我的视图控制器中同时出现了 UIImage 和“后退”按钮,但不知道为什么。

这是一个屏幕截图:

在此处输入图像描述

这是我的代码:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self.navigationController setNavigationBarHidden:NO animated:YES];

    UIImage *image = [UIImage imageNamed:@"arrow_back.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

    [self.navigationController.navigationBar addSubview:imageView];

}

谢谢你的帮助。

4

3 回答 3

2

您必须创建一个 backbarbutton 项目并为其添加自定义项。然后为它定义动作,在其中弹出当前视图。

 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;


// selector for backButton

-(void)back{
[self.navigationController popViewController];
}
于 2012-05-15T20:36:03.420 回答
0

您需要创建一个自定义按钮并将其添加为 self.navigationController.navigationBar.backBarButtonItem 的子视图。

于 2012-05-15T20:27:54.323 回答
0

当您将第二个视图控制器推入导航控制器时,后退按钮是自动的。您可以像 Andrew Ebling 提到的那样覆盖它。您将需要创建一个自定义 UIBarButton 项。

UIImage *image = [UIImage imageNamed:@"back.png"];
UIBarButtonItem *back = [UIBarButtonItem alloc] initWithImage:image
                                                        style:UIBarButtonItemStylePlain
                                                       target:self
                                                       action:@selector(backEvent:)];

self.navigationItem.backBarButtonItem = back;
于 2012-05-15T20:31:07.027 回答