1

I am creating a taller navigation bar, height == 200, however, when i clicked below the back button, it also navigates back.

enter image description here

here is my code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;

    navBar = [[SRNavigationBar alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 200.0)];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [self.navigationItem setHidesBackButton:YES animated:YES];

    __weak id weakSelf = self;
    self.navigationController.interactivePopGestureRecognizer.delegate = weakSelf;

[self styleNavBar];
}

- (void)styleNavBar
{
    UINavigationItem *newItem = [[UINavigationItem alloc]initWithTitle:[[PFUser currentUser] objectForKey:@"nickName"]];

    UIBarButtonItem *menu = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    newItem.leftBarButtonItem = menu;
    newItem.leftBarButtonItem.tintColor = [UIColor colorWithRed:245/255.0 green:124/255.0 blue:0/255.0 alpha:1];

    [navBar setItems:@[newItem]];

    [self.view addSubview:navBar];
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

Any help will be appreciated

Moin Shirazi
  • 4,372
  • 2
  • 26
  • 38
Dovahkiin
  • 1,239
  • 11
  • 23

2 Answers2

0

you can use custom button giving height as per your need

-(void)addLeftButton
{
     UIImage *buttonImage = [UIImage imageNamed:@"btn_back.png"];

     UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];

     [aButton setBackgroundImage:buttonImage forState:UIControlStateNormal];

     aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, 200.0);

     UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];

     [aButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];

     [self.navigationItem setLeftBarButtonItem:aBarButtonItem];
}
Moin Shirazi
  • 4,372
  • 2
  • 26
  • 38
  • I just want to navigate back when I click exactly on the image, not anywhere below. My question is, why does it navigate back when i even click below it? – Dovahkiin Oct 12 '15 at 03:21
  • Then add a fix height to it so that it wont effect on below part of button – Moin Shirazi Oct 12 '15 at 03:29
  • no it won't work. I checked the view hierarchy, the button frame is exactly what i want, but still, the entire area below it is still clickable – Dovahkiin Oct 12 '15 at 03:30
0

Apple Support:

I recommend that you avoid having touch-sensitive UI in such close proximity to the nav bar or toolbar. These areas are typically known as "slop factors" making it easier for users to perform touch events on buttons without the difficulty of performing precision touches. This is also the case for UIButtons for example.

But if you want to capture the touch event before the navigation bar or toolbar receives it, you can subclass UIWindow and override: -(void)sendEvent:(UIEvent *)event;

https://stackoverflow.com/a/9719364/2138564

Community
  • 1
  • 1
Lida Zhu
  • 21
  • 6