Override back button in navigation stack while keeping appearance of default back button?

后端 未结 8 2080
野的像风
野的像风 2020-12-08 22:38

How do I override the back button for just one view (not for all the back buttons present in different views) such that on click of the back button, root view controller is

8条回答
  •  一向
    一向 (楼主)
    2020-12-08 23:00

    For keeping same appearance you can use :

    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;
    

提交回复
热议问题