disable multiple touches on UiNavigationBar - ios

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;

Related

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

Intercept back navigation in iOS7

I want to stop somebody going back on my Navigation Controller if they haven't saved their changes. Most posts on SO discuss overriding the back button (with a variety of techniques to do this). However, iOS7 allows you to now swipe to go back in a navigationViewController...
I did see the UINavigationControllerDelegate which looks like the right type of delegate I'd want to implement but I see no way to cancel a navigation action. Any ideas how to do this?
Unfortunately nothing changed in iOS7, you still need to fake your back button if you want to put some check into it.
By the way it easier now since you don't need to fake the arrow button.
Edit:
to do that:
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 44.0f, 30.0f)];
[backButton setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(popVC) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

Adding action and target to a custom Navigation BarButtonItem?

I am trying to customize the Navigation Bar by adding a custom button to the rightBarButtonItem. Creating one is pretty straight forward.
UIImage *myImage = [UIImage imageNamed:#"menu-icon.png"];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setFrame:CGRectMake(0, 0, myImage.size.width, myImage.size.height)];
[myButton setBackgroundImage:myImage forState:UIControlStateNormal];
UIBarButtonItem *myButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myButton];
[navItem setLeftBarButtonItem:myButtonItem animated:YES];
Looks good, except that now I can't set the target or action properties. The following code does nothing:
myButtonItem.target = myTarget;
myButtonItem.action = #selector(myButtonAction);
I found that the reason it's not working is because I initialized the UIBarButtonItem with -initWithCustomView. Apples documentation says:
The bar button item created by this method does not call the action method of its target in response to user interactions. Instead, the bar button item expects the specified custom view to handle any user interactions and provide an appropriate response.
I'm a little confused by what exactly apple means by that. Am I supposed to create a custom control or something? I haven't really done that yet and I'm not sure where to start.
Does anyone have any suggestions on how to do this or an alternative way to set up a custom Navigation Bar Button?
ANSWERED----------------------------------------------------------
Just had to add this method to the UIButton:
[myButton addTarget:myTarget action:#selector(myButtonAction) forControlEvents:UIControlEventTouchUpInside];
It's pretty straight forward, you need to add target for uibutton not for uibarbuttonitem.
To explain more what you want for uibarbuttonitem is respond touch up inside action method, as documentation says that you need to do that for uibutton as you are initializing your uibarbuttonitem with custom view in your case uibutton.
Add an action and target to a UIButton like this:
[myButton addTarget:self
action:#selector(handleMyButtonTap)
forControlEvents:UIControlEventTouchUpInside];
Add an action and target to a UIBarButtonItem like this:
[myBarButtonItem setAction:#selector(handleBarButtonTap)];
[myBarButtonItem setTarget:aTarget];
With the help of answers by hgwhittle and Mustafa I implemented and it works perfect
here is the complete snip may help someone
//Create ImageButton
UIImage *shareBtnIcon = [UIImage imageNamed:#"dummy_share_icon_22"];
UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
[shareButton setFrame:CGRectMake(0, 0, shareBtnIcon.size.width, shareBtnIcon.size.height)];
[shareButton setBackgroundImage:shareBtnIcon forState:UIControlStateNormal];
[shareButton addTarget:self action:#selector(shareButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
//Create BarButton using ImageButton
UIBarButtonItem *shareBarBtn = [[UIBarButtonItem alloc] initWithCustomView:shareButton];
//Add BarButton to NavigationBar
self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:shareBarBtn, nil];
//method for share butto click
-(void) shareButtonClicked:(UIButton*)sender
{
NSLog(#"you clicked on share button %ld");
}

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

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.

iOS Custom Back Button without calling method in every View Controller

I would like a more elegant solution to changing the back button for an Application's Navigation Bar. All the answers I've seen only talk about customizing a button and then adding at least one line of code to each View Controller for the new button to display.
I have a category where I modify the Navigation Bar's background and Title. It's a category of UINavigationBar. The category calls willMoveToWindow: to call methods each time a new view is pushed on the Navigation Controller.
So here's how the code to create a new button would look:
- (void)applyCustomButton{
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 26, 26)];
[backButton setImage:[UIImage imageNamed:#"arrow-left"] forState:UIControlStateNormal];
[backButton setShowsTouchWhenHighlighted:TRUE];
[backButton addTarget:self action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
[self.navigationItem setLeftBarButtonItem:barButtonItem];
}
The problem here is that self.navigationItem won't work here. I don't want to bore you with all the things I tried categorizing, but that property only works on View Controllers (I think)?
You don't have to give me a solution for the code above, maybe you have a better way of customizing the Back Button on the Navigation Bar without having to write code on every View Controller Class I have. I just hate repetition because maintaining code like that sucks.
Thanks in advance.

Resources