我的 View Controller 中同时出现了一个 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];
}
感谢您的帮助。
最佳答案
您必须创建一个后退栏按钮项并向其添加自定义项。然后为其定义操作,弹出当前 View 。
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];
}
关于iphone - UIImage 和 "back"按钮都出现在我的 viewController 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10608302/