覆盖导航堆栈中的后退按钮,同时保持默认后退按钮的外观?

时间:2011-02-22 23:48:35

标签: iphone

如何仅覆盖一个视图的后退按钮(不适用于不同视图中的所有后退按钮),以便在单击后退按钮时显示根视图控制器。

9 个答案:

答案 0 :(得分:32)

您需要替换后退按钮并关联操作处理程序:

- (void)viewDidLoad {
    [super viewDidLoad];

    // change the back button to cancel and add an event handler
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@”back”
                                                                   style:UIBarButtonItemStyleBordered
                                                                  target:self
                                                                  action:@selector(handleBack:)];

    self.navigationItem.leftBarButtonItem = backButton;
}

- (void)handleBack:(id)sender {
    // pop to root view controller
    [self.navigationController popToRootViewControllerAnimated:YES];
}

答案 1 :(得分:12)

找到了一个保留后退按钮样式的解决方案。 将以下方法添加到视图控制器。

-(void) overrideBack{

    UIButton *transparentButton = [[UIButton alloc] init];
    [transparentButton setFrame:CGRectMake(0,0, 50, 40)];
    [transparentButton setBackgroundColor:[UIColor clearColor]];
    [transparentButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationController.navigationBar addSubview:transparentButton];


}

现在根据需要提供以下方法中的功能:

-(void)backAction:(UIBarButtonItem *)sender {
    //Your functionality
}

所有这一切都是用透明按钮覆盖后退按钮;)

答案 2 :(得分:6)

它已经过时了,但正确的答案是:

除了将ViewController推到其他所有其他方面之外,您最好只使用rootVC和新VC替换完整堆栈。

self.navigationController?.pushViewController(myVc, animated: true)

可是:

let vcStack = self.navigationController?.viewControllers
self.navigationController?.setViewControllers([vcStack![0],myVc], animated: true)

就像那样,在背面它只是popToRoot,因为它是堆栈中的前一个viewController

答案 3 :(得分:6)

我有类似的问题,我成功地使用了Sarasranglt给出的答案。这是SWIFT版本。

    override func viewDidLoad() {
       let transparentButton = UIButton()
       transparentButton.frame = CGRectMake(0, 0, 50, 40)
       transparentButton.backgroundColor = UIColor.orangeColor()
       transparentButton.addTarget(self, action:"backAction:", forControlEvents:.TouchUpInside)
       self.navigationController?.navigationBar.addSubview(transparentButton)
    }

功能是

 func backAction(sender:UIButton) {
     // Some sction
 }

答案 4 :(得分:2)

另一种方法是采用UINavigationControllerDelegate协议。

– navigationController:willShowViewController:animated:
– navigationController:didShowViewController:animated:

当控制器出现时,这些方法会通知您,但您必须检查控制器是否是您想要的控制器。

答案 5 :(得分:0)

要保持外观不变,可以使用:

UIView *leftButtonView = [[UIView alloc]initWithFrame:CGRectMake(-12, 0, 105, 30)];

UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
leftButton.frame = leftButtonView.frame;
[leftButton setImage:[UIImage imageNamed:@"ic_system_back"] forState:UIControlStateNormal];
[leftButton setTitle:@"title" forState:UIControlStateNormal];
[leftButton.titleLabel setLineBreakMode:NSLineBreakByTruncatingTail];
[leftButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
[leftButton setTitleEdgeInsets: UIEdgeInsetsMake(0, 8, 0, 0)];
[leftButton addTarget:self action:@selector(handleBack:) forControlEvents:UIControlEventTouchUpInside];
[leftButtonView addSubview:leftButton];

UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithCustomView:leftButtonView];
self.navigationItem.leftBarButtonItem = leftBarButton;

答案 6 :(得分:0)

遵循Sarasranglt在目标C中具有透明按钮的方法以及Pavle Mijatovic的早期Swift版本,以下是Swift 4版本:

 res = pd.crosstab(index=R['Unique household identifier'], 
                   columns=R['Relationship to head [Standardized version]'])
 res.drop('Head', axis=1)

Relationship to head [Standardized version]  Son/  Spou
Unique household identifier                            
1                                               4     1
2                                               2     1

let transparentButton = UIButton()
transparentButton.frame = CGRect(x:0, y:0, width:50, height: 40)
transparentButton.backgroundColor = UIColor.clear
transparentButton.addTarget(self, action:#selector(backAction(sender:)), for:.touchUpInside)
self.navigationController?.navigationBar.addSubview(transparentButton)

答案 7 :(得分:0)

无需制作自定义按钮或没有任何 hack(transparentButton) 只需覆盖 'viewWillDisappear' 方法并在块内编写代码。

override func viewWillDisappear(_ animated: Bool) {
        // write your code...
}

答案 8 :(得分:-1)

使用此代码显示自定义后退按钮,在将其标记为重复答案之前请注意backBarButtonItem

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"< back"
                                                               style:UIBarButtonItemStylePlain
                                                              target:self
                                                              action:@selector(handleBack:)];

self.navigationItem.backBarButtonItem= backButton;

干杯!