1
votes
  1. I have implement custom back Navigation bar button.

  2. Codes:

    -(UIBarButtonItem*) logicToAddBackButton {

    UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UiNavigationBack"]];
    
    UILabel *label=[[UILabel alloc] init];
    
    [label setTextColor:[UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0]];
    [label setText:@"Home"];
    [label sizeToFit];
    
    int space=6;
    label.frame=CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+space, label.frame.origin.y, label.frame.size.width, label.frame.size.height);
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, label.frame.size.width+imageView.frame.size.width+space, imageView.frame.size.height)];
    
    view.bounds=CGRectMake(view.bounds.origin.x+8, view.bounds.origin.y-1, view.bounds.size.width, view.bounds.size.height);
    [view addSubview:imageView];
    [view addSubview:label];
    
    UIButton *button=[[UIButton alloc] initWithFrame:view.frame];
    [button addTarget:self action:@selector(eventBack) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    
    [UIView animateWithDuration:0.33 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        label.alpha = 0.0;
        CGRect orig=label.frame;
        label.frame=CGRectMake(label.frame.origin.x+25, label.frame.origin.y, label.frame.size.width, label.frame.size.height);
        label.alpha = 1.0;
        label.frame=orig;
    } completion:nil];
    
    UIBarButtonItem *backButton =[[UIBarButtonItem alloc] initWithCustomView:view];
    
    return backButton;
    

    }

    self.navigationItem.leftBarButtonItem = [self logicToAddBackButton];

  3. This is how it look and work fine according to the logic. enter image description here

  4. Issue: If we click on first half of arrow, the back button do not respond.

  5. Please suggest on this.
2

2 Answers

3
votes

Try to set to your button:

button.contentEdgeInsets = UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
0
votes

I think you miscalculating frame somewhere. Why don't you add UIButton instead? It will be much easier, but without label animation.

  UIButton *button = [[UIButton alloc] init];
    [button addTarget:self action:@selector(eventBack:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"Home" forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"UiNavigationBack"] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0] forState:UIControlStateNormal];
    [button sizeToFit];
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:button];

    return backButton;