简体   繁体   中英

Custom navigation bar back button hit area

  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. 在此输入图像描述

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

  5. Please suggest on this.

尝试设置为您的按钮:

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM