I've been working on my book app, but I have a UIBarButtonItem dilemma. I have set the 3 bar buttons on the toolbar as you can see in the image below. They work fine until I set the single tap recognizer, the last 2 lines in the code. What I want to do is get out of this modal screen by a single tap, not a double tap, not by adding another 'Cancel' button on the toolbar. After I set the single tap recognizer, none of the bar button items work.
How can I get out of this dilemma. Can any one tell me how to get around this? Thank you for your time.
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleDefault;
[toolbar sizeToFit];
toolbar.frame = CGRectMake(0, 410, 320, 50);
toolbar.tintColor = _label.backgroundColor;
self.title = #"Test For Toolbar";
UIBarButtonItem* TofC = [[UIBarButtonItem alloc] initWithTitle:#"T of C"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(showTofC)];
UIBarButtonItem* bookMark =
[[UIBarButtonItem alloc] initWithTitle:#"Book Mark"style:UIBarButtonItemStyleBordered
target:self
action:#selector(bookMarkIt)];
UIBarButtonItem* searchBtn = [[UIBarButtonItem alloc] initWithTitle:#"Search"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(searchIt)];
UIBarButtonItem* spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self
action:nil];
NSArray* buttons =
[NSArray arrayWithObjects:TofC, spacer, bookMark, spacer, searchBtn, nil];
self.navigationController.toolbarHidden = NO;
[toolbar setItems:buttons animated:NO];
[self.view addSubview:toolbar];
/*
// Single Tap
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleTapGesture:)];
[self.view addGestureRecognizer:tapGesture];
*/
// Single Tap
Dum2ViewController* dum2ViewController = [[Dum2ViewController alloc] init];
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]
initWithTarget:dum2ViewController
action:#selector(handleTapGesture:)];
[dum2ViewController.view addGestureRecognizer:tapGesture];
You've added the toolbar as a subview of your main view and then attached a gesture recognizer to the main view.
So as soon as you touch the toolbar, the touch goes to the main view first and is intercepted the gesture recognizer before it can be passed down to the toolbar items.
What I'd recommend doing is setting a gesture recognizer on whatever content (another subview?) that is inside your current view. That way a touch in the toolbar will be routed to the toolbar and a touch within the view will go to the (content) subview and be caught by the gesture recognizer.
Makes sense?
Related
I have had a medical calculation app for cardiac ultrasound in the App Store since 2011. At the bottom of each data entry screen is a toolbar with buttons to analyze all data, designate the open calculation as a favorite, and clear all active data from all calculations. After the most recent iOS update to 13.4.1, the Clear button at the bottom right of the screen has become unresponsive. If I put the app in the background, open another app, and then switch back to my app, then the Clear button becomes responsive again. If I move the Clear button to the left side of the toolbar, next to the Analyze button, then it functions without a problem. Since the code that displays these buttons has been the same for years and multiple iOS versions, I am wondering if possibly there is a bug in iOS 13.4.1 causing this issue.
Here is the code used to generate the toolbar:
//Initialize the toolbar
toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleDefault;
//Set the toolbar to fit the width of the app.
[toolbar sizeToFit];
//Caclulate the height of the toolbar
CGFloat toolbarHeight = [toolbar frame].size.height;
//Get the bounds of the parent view
CGRect rootViewBounds = self.parentViewController.view.bounds;
//Get the height of the parent view.
CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
//Get the width of the parent view,
CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
//Create a rectangle for the toolbar
CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight - [sharedVariableState g_toolbar_adjuster_for_Home_indicator], rootViewWidth, toolbarHeight);
//Reposition and resize the receiver
[toolbar setFrame:rectArea];
//Create two buttons separated by spacer item.
UIBarButtonItem *analyzeButton = [[UIBarButtonItem alloc] initWithTitle:#"Analyze" style:UIBarButtonItemStylePlain target:self action:#selector(analyze_clicked:)];
UIBarButtonItem *clearButton = [[UIBarButtonItem alloc] initWithTitle:#"Clear" style:UIBarButtonItemStylePlain target:self action:#selector(clear_clicked:)];
//Use this to put space in between your toolbox buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; //Load NSUserDefaults.
NSMutableArray *objectArrayMutable = [[NSMutableArray alloc]init];
objectArrayMutable = [[prefs objectForKey:#"savedFavoritesArray"] mutableCopy];
int favoriteChosenAlready = 0;
if ([objectArrayMutable count] > 0) {
for (int arrayCounter = 0; arrayCounter < [objectArrayMutable count]; arrayCounter++) {
if ([[objectArrayMutable objectAtIndex:arrayCounter] integerValue] == 58) {
favoriteChosenAlready = 1;
}
}
}
UIButton *favButton = [[UIButton alloc] init];
if (favoriteChosenAlready == 0) {
//Display empty star.
[favButton setImage:[UIImage imageNamed:#"Star_button_empty.png"] forState:UIControlStateNormal];
}
else {
//Display filled star.
[favButton setImage:[UIImage imageNamed:#"Star_button_filled.png"] forState:UIControlStateNormal];
}
[favButton addTarget:self action:#selector(favorite_clicked:) forControlEvents:UIControlEventTouchUpInside];
[favButton setFrame:CGRectMake(280, 25, 30, 30)];
UIBarButtonItem *favoriteButton = [[UIBarButtonItem alloc] initWithCustomView:favButton];
[[UIBarButtonItem appearance] setTintColor:[UIColor lightGrayColor]]; //Make toolbar button text lightGrayColor.
[toolbar setItems:[NSArray arrayWithObjects:analyzeButton,flexItem,favoriteButton,flexItem,clearButton,nil]];
//Add the toolbar as a subview to the navigation controller.
[self.navigationController.view addSubview:toolbar];
To clarify, the Clear button here now is unresponsive:
[toolbar setItems:[NSArray arrayWithObjects:analyzeButton,flexItem,favoriteButton,flexItem,clearButton,nil]];
However, just rearranging the sequence of buttons in the toolbar fixes the problem:
[toolbar setItems:[NSArray arrayWithObjects:analyzeButton,clearButton,favoriteButton,flexItem,flexItem,nil]];
I want to change leftBarButton title. I've tried this
UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
[[self navigationItem] setLeftBarButtonItem:newBackButton];
but it brings changes like
But I want it like
How it can be possible?
It may helpful for you
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleDone target:self action:#selector(backAction:)];
self.navigationItem.leftBarButtonItem = backBarButton;
From the view controller (ViewController1) that is pushing the next view controller (ViewController2), you need to set the backBarButtonItem.
#implementation ViewController1
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"New Title" style:UIBarButtonItemStylePlain target:nil action:nil];
}
It's totally normal that you don't see any arrow in your back button. That's because you're creating a new bar button that you set its title and its style. But the style you've set for your bar button item won't give you what you wanted.
I suppose that you're using UINavigationController to navigate. If it's the case, you need to get a reference to navigationBar that UINavigationController displays on the screen. Then, you'll be able to change its color via tint color property. So,
[self.navigationController.navigationBar setTintColor:[UIColor redColor]];
would do what you want to achieve. Here is the image i obtained using the code i mentioned above.
If you're not using UINavigationController, the simplest solution is to provide an image for your back button which has already an arrow.
You should set backBarButtonItem instead of leftBarButtonItem. And remember to set it not in current view controller (where you wish it to be displayed) but in parent one (to which this button will return you).
Use this Code and
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:#"BackBtn.png"] ;
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn addTarget:self action:#selector(goback) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0, 54, 30);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
self.navigationItem.leftBarButtonItem = backButton;
and for going to back use this method .
-(void)goback
{
[self.navigationController popViewControllerAnimated:YES];
}
This worked for me
UIImage *image = [[UIImage imageNamed:#"btn_back.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:#selector(backAction:)];
self.navigationItem.leftBarButtonItem = button;
- (IBAction)backAction:(id)sender
{
MainMenuViewController *mainMenuViewController = [[MainMenuViewController alloc] init];
[self.navigationController pushViewController:mainMenuViewController animated:YES];
}
bar button got stick to the y origin as 0 to navigation bar and x as 0.but i want to add some y position to barbutton item.
UIBarButtonItem *barbutton;
barbutton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(onbackbtnpressed:)];
[barbutton setWidth:30];
[barbutton setTintColor:KANEKA_BLUE_COLOR];
[barbutton setAccessibilityFrame:CGRectMake(10, 10, 30, 20)];
self.navigationItem.leftBarButtonItem = barbutton;
Please tell me how can i set the barbutton frame or how to set the bar button y position.
Actually I have managed to set the y origin in little bit different way may be it is big procedure but it is quite good.
I have added a toolbar to viewcontroller and set the bar button item and hidden the navigation bar.And it works for all the popover controllers.
self.toolbar.hidden = NO;
self.contentSizeForViewInPopover = CGSizeMake(self.view.frame.size.width-100,self.view.frame.size.height-100);
self.navigationController.navigationBarHidden = YES;
[self.toolbar setTintColor:KANEKA_BLUE_COLOR];
[self.backbutton setTintColor:KANEKA_BLUE_COLOR];
My views about this question - As per iOS Human Interface Guidelines it is not recommended to add y spacing for the bar button items on navigation bar.
We can update x positioning by setting an array of buttons for navigation item as shown below:
UIBarButtonItem *fixedSpaceBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[fixedSpaceBarButton setWidth:10];
UIBarButtonItem *barbutton;
barbutton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(onbackbtnpressed:)];
[barbutton setWidth:30];
[barbutton setTintColor:[UIColor redColor]];
[barbutton setAccessibilityFrame:CGRectMake(10, 10, 30, 20)];
self.navigationItem.leftBarButtonItems = [NSArray
arrayWithObjects:fixedSpaceBarButton, barbutton, nil];
I have an existing code which was working fine in ios6. but in ios7 left most item("refresh button") is not showing align to other two UIBarButtonItem . its showing little down. here is the code for iOS6. what changes do i need to make this working in iOS7.
// create an array for the buttons
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard save button
UIBarButtonItem* refreshButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:#selector(refreshButtonClicked:)];
refreshButton.style=UIBarButtonItemStyleBordered;
//self.navigationItem.rightBarButtonItem = refreshButton;
[buttons addObject:refreshButton];
[refreshButton release];
// create a spacer between the buttons
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
[spacer release];
// create a standard delete button with the trash icon
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark];
[button setFrame:CGRectMake(0, 0, 30, 30)];
[button addTarget:self action:#selector(InfoButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
infoItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[buttons addObject:infoItem];
// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:NO];
[buttons release];
// place the toolbar into the navigation bar
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithCustomView:toolbar]autorelease];
Thanks
Try:
self.navigationItem.rightBarButtonItems = buttons;
In Safari, when I press a dropdown box, a picker comes up with a navigation bar on top of it that has a done button and some other buttons.
I want a similar bar with two buttons above my picker view, but I can't figure out how to put buttons on the bar. With a navigation controller, I'd do:
self.navigationItem.rightBarButtonItem = [UIButton .....
self.navigationItem.backBarButtonItem = [UIButton ......
But I'm not finding any button properties on the navigation bar that I'm creating programmatically. Adding the buttons as subviews also doesn't seem to be doing anything. How can I add them?
It’s a toolbar, not a navigation bar. Set its items property to populate it with UIBarButtonItem objects. Note that those are not the same as UIButtons, and in fact don’t even subclass from UIView; they have their own set of attributes. A simple example:
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolbar.items = [NSArray arrayWithObjects:
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease],
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed)] autorelease],
nil];
someTextField.inputAccessoryView = toolbar;
[toolbar release];