Navigation bar button click event firing when clicked on area below it. - ios

I am having navigation bar with custom view as right bar button item. Custom view has two buttons in it. But when i touch area below navigation bar on right side it fires button touch event. I have checked frames of buttons and view by changing background color all are set perfectly.
I checked for default iOS apps like clock, calendar, setting, notes etc they all have same behaviour. Touching in red rectangle in screenshots(of default iOS calendar app.) fires touch event of button same is happening with my app too..
Here is code..
UIView* navigationButtonsView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 44.0f)];
UIButton *p = [UIButton buttonWithType:UIButtonTypeCustom];
[p setImage:[UIImage imageNamed:#"PBtn.png"] forState:UIControlStateNormal];
[p addTarget:self action:#selector(PButtonPushed:) forControlEvents:UIControlEventTouchUpInside];
[p setFrame:CGRectMake(43, 0, 57, 44)];
[navigationButtonsView addSubview:p];
UIButton *o = [UIButton buttonWithType:UIButtonTypeCustom];
[o addTarget:self action:#selector(oButtonPushed:) forControlEvents:UIControlEventTouchUpInside];
[o setFrame:CGRectMake(0, 0, 38, 44)];
[o setImage:[UIImage imageNamed:#"O.png"] forState:UIControlStateNormal];
[navigationButtonsView addSubview:o];
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:navigationButtonsView];
[musicVC.navigationItem setRightBarButtonItem: barItem animated:NO];
How to change this default iOS behaviour for navigation bar buttons ? Because m having some UI right below these buttons and its top area not responding because of this.
Thanks in advance..

This is normal behavior - two reasons:
iOS doesn't know how big your finger is - so it has to decide where to put the touch. In this case, nav bar overrides table view.
This may have something to do with the Human Interface Guidelines (sorry, no link. Google it) that say that the minimum touchable area must be 44px. This behavior may be the 'UIBarButtonItem' trying to conform with these requirements.

Related

Making the UINavigationBar Shorter

I have seen several apps that have shorter UINavigationBar. Like the StackOverFlow iOS app.
How do we achieve this?
Try this:
-(void)viewDidLayoutSubviews{
CGRect temp=self.navigationController.navigationBar.frame;
[self.navigationController.navigationBar setFrame:CGRectMake(temp.origin.x, 40, temp.size.width, temp.size.height)];
}
If you open several apps with navigation bars, you can see your statement is wrong when double-tapping the home button (to see them lined up nicely).
Compare Facebook, Whatsapp, YouTube, Pages, and Messages for example, they're all the same size. So unless they're all shrunk to the same size, I'd say they're not shrunk at all !
Now on top of that I suggest that you don't "change" your navigation bar too much, as it is a strong pivot point for your user and changing it would be "disturbing". Customize it to your will but have some limits.
If you just want to reduce it's height, I think you can simply use the navigationbar properties :
self.navigationController.navigationBar setFrame:<#(CGRect)#>
or
self.navigationController.navigationBar.frame.size.height = //yourFloatValue.
And that should do it !
As per my knowledge, I don't believe that you can edit the hight of navigation Bar (For UX purposes). But you can hide it and create your own custom view and place it on top of the screen.
You can use this code to hide the navigation bar
self.navigationController.navigationBar.hidden = YES;
and you create your own custom view like follows
CGRect barFrame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 65);
UIView *customeBar = [[UIView alloc] initWithFrame:barFrame];
[self.view addSubview:customeBar];
UIButton *navButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[navButton addTarget:self action:#selector(aMethod:) forControlEvents:UIControlEventTouchUpInside];
[navButton setTitle:#"" forState:UIControlStateNormal];
[navButton setBackgroundImage:[UIImage imageNamed:#"someImage.png"] forState:UIControlStateNormal];
navButton.frame = CGRectMake(10, 10, 56.0, 56.0);
[customeBar addSubview:navButton];

disable multiple touches on UiNavigationBar

I have left and right navigation bar buttons on navigation bar, both can be tapped at the same time, and their action gets fired, How can I prevent multiple touch. I have searched over here and google, but no success
You can also use -setExclusiveTouch on each of the buttons so that only one will register at a time. You don't need to disable multi-touch on your view either.
Pretty much simple you can use make use of ExclusiveTouch property in this case
[self.navigationController.navigationBar setExclusiveTouch:YES];
This is a Boolean value that indicates whether the receiver handles touch events exclusively.
Setting this property to YES causes the receiver to block the delivery of touch events to other views in the same window. The default value of this property is NO.
There is a property called exclusiveTouch for UIButton. Create a navigation button using that like so:
Eg:
UIButton *btnAdd = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 38, 37)];
[btnAdd setContentMode:UIViewContentModeScaleAspectFit];
[btnAdd setBackgroundImage:[UIImage imageNamed:#"abc.png"] forState:UIControlStateNormal];
[btnAdd addTarget:selfaction:#selector(<method>) forControlEvents:UIControlEventTouchUpInside];
[btnAdd setExclusiveTouch:YES];
UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithCustomView:btnAdd];
self.navigationItem.rightBarButtonItem = rightBtn;

Custom navigation bar not showing

Controller 1 displays an array of UIImages, Controller 2 manages the paging of those images once selected, and Controller 3 just displays the images for the pages. When I tap on a cell and load a photo into the photoViewController, the paging of images works fine, but my custom navigation bar is overridden with the default bar and I am unable to add any custom buttons to it or change any of its properties in code. The only thing I found that I am able to do is hide it. If I try to change the title of the back button or add my own title to the navigation bar nothing changes. Please help with anything you can.
i think you should try like this
self.navigationItem.leftBarButtonItem = [self leftMenuBarButtonItem];
and function could be
- (UIBarButtonItem *)leftMenuBarButtonItem {
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 50, 30)]; [backButton setImage:[UIImage imageNamed:#"bar_btn_bg.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(leftDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
return [[UIBarButtonItem alloc] initWithCustomView:backButton];
}
if you fall in between let me know

Change navigation bar back button and remove title to make it appear like in ios7

I want to remove back button title from my navigation bar and put a custom back button to make it appear as in ios7 (just a left arrow). I could change change the text, but when i change the text to nil, the previous page title appears again. Also changing the background image is not working too, though i could change the image
try this code to hide back button:
self.navigationItem.hidesBackButton=YES;
self.navigationItem.leftBarButtonItem=nil;
create custom back button like this:
CGRect frameimgback1 = CGRectMake(0, 0, 60, 35);
UIButton *back = [[UIButton alloc] initWithFrame:frameimgback1];
[back setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[back addTarget:self action:#selector(back)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnL = [[UIBarButtonItem alloc]initWithCustomView:back];
self.navigationItem.leftBarButtonItem = btnL;

How to create Navigation bar with back button on it?

I am very new to iOS App Development.
In my app I am trying to navigate from second view to first view using navigation bar back button, but while drag and drop the navigation bar in the view I am just able to get the bar with title.
I am really worried and I am not sure what I am doing wrong? I am using Xcode 4.3. and storyboard to design the view.
What i am getting now
Looking for
Thanks for your help guys
Xcode handles this for you.
Highlight your first controller. Go to the menu bar and click Embed In > Navigation Controller. Now using UIButton or something, control-click and drag from your first view controller's button to your second view controller and make it a Push segue. The back button won't appear, but if you run it, it will be there.
So you should have 3 views in storyboard. The Navigation Controller, your first view controller, and your second view controller.
Here is one way to do it using code.
if you want to get a plainButton in the navigation like the iphone`s backButton (only one view), you must add image, i think there is no other way.Of course , if you have 2 or more viewcontrollers , you will use navigationItem.backBarButtonItem without add image .But the first viewcontroller do not have the backButton.
the code like this:
navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UINavigationItem *navigationItem = [[[UINavigationItem alloc] initWithTitle:#"Detail"] autorelease];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
[button setImage:[UIImage imageNamed:#"plain.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]
initWithCustomView:button];
navigationItem.leftBarButtonItem = buttonItem;
[buttonItem release];
[button release];
[navigationBar pushNavigationItem:navigationItem animated:NO];
[self.view addSubview:navigationBar];

Resources