How to show Done on Decimal Pad? - ios

I have a UITextField which is Number Pad and its properties look like
But when I run my application, I do not see Done on my screen
What is the issue? How can I get Done on Decimal Pad?

The best way I've found when doing something like this is to append an inputAccessoryView to the UITextField. That input accessory view can have a "Done" button on it that dismisses the keyboard for you. I don't think you should put a "Done" button on the key pad itself, because where would it go?
Here's how I've done it before:
- (void)setupKeyboardAccessory {
UIToolbar *keyboardToolbar = [[UIToolbar alloc] init];
[keyboardToolbar sizeToFit];
keyboardToolbar.backgroundColor = [UIColor whiteColor];
UIBarButtonItem *flexBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self.patientAgeTextField action:#selector(resignFirstResponder)];
keyboardToolbar.items = #[flexBarButton, doneBarButton];
self.textField.inputAccessoryView = keyboardToolbar;
}

Related

Keyboard toolbar for multiple textFields

In my project, some viewControllers have multiple text fields,
I found how to add a toolbar above keyboard with a "Ok" button to hide keyboard when the button is tapped.
The code I am using is this below :
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:_destField action:#selector(resignFirstResponder)];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 34)];
toolbar.items = [NSArray arrayWithObjects:flex, barButton, nil];
_destField.inputAccessoryView = toolbar;
How can I easily reuse this code in the same view controller?
The "target" makes this difficult, is there a way without creating a toolbar for each textField?
Thanks!
If this code is in a view controller then there is a simple solution. Change the barButton to something like this:
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissKeyboard)];
Then add this method to the view controller:
- (void)dismissKeyboard {
[self.view endEditing:YES];
}
That will dismiss the keyboard no matter what view is showing it.
Now you can reuse that toolbar as the inputAccessoryView for any text field/view in the view controller.

UIBarButtonItem Not Working Input Accesory View iOS 7

I have a dismiss button that is a UIBarButtonItem, which is added to a UIToolbar, which is added to a text boxes inputAccessoryView.
It worked find in iOS 6, but since iOS 7 the Button doesn't act like a button, its just text.
Here is what it looks like in iOS 6.
Here is what it looks like in iOS 7
It doesn't act like a button anymore, and when I try to click it I just click the icon behind the Dismiss text.
Here is the code for this portion of the app:
self.searchBox.clearButtonMode = UITextFieldViewModeWhileEditing;
self.searchBox.returnKeyType = UIReturnKeySearch;
self.searchBox.delegate = self;
UIToolbar* searchToolbar = [[UIToolbar alloc]init];
searchToolbar.tintColor = [UIColor blackColor];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:#"Dismiss" style:UIBarButtonItemStyleDone target:self action:#selector(doneWithKeyboard)];
[searchToolbar setItems:[NSArray arrayWithObjects:flexibleSpace, doneButton, nil]];
self.searchBox.inputAccessoryView = searchToolbar;
Any help will be appreciated.

UIToolBar with UIBarButtonItems equally spaced regardless of title length

So I'm tasked with adding buttons to a UIToolBar and setting the inputAccessoryView of a textView to this UIToolBar.
so I did the basics:
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:#"title1" style:UIBarButtonItemStylePlain target:self action:selector];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:#"longer title2" style:UIBarButtonItemStylePlain target:self action:selector];
UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithTitle:#"title 3 is a lot longer" style:UIBarButtonItemStylePlain target:self action:selector];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *divider1 = [self barButtonDivider];
UIBarButtonItem *divider2 = [self barButtonDivider];
UIToolBar *toolBar = [UIToolBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
[toolBar setItems:#[spacer,item1,spacer,divider1,spacer,item2,spacer,divider2,spacer,item3,spacer];
self.textView.inputAccessoryView = toolbar;
So there are more approaches to this.
1) I could subclass a UIToolBar
2) I could subclass a UIView and set a view to be the size I need (width of the superView and height 44), but I'd need to do all of this in Auto-Layout which I tried and could not get right.
Is there a way I can use the above method with a standard ToolBar and get the right spacing where all the buttons no matter how many I use are equal widths?
ETA:, tried the below solution and didn't work:
- (UIBarButtonItem *)spacer {
return [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
}
[toolBar setItems:#[[self spacer], item1, [self spacer], divider1, [self spacer], item2, [self spacer], divider2, [self spacer], item3, [self spacer]];
Resolved with this solution but I don't like it:
button.width = 150;
the buttons would remain equal in portrait or landscape so I guess this is a working solution what do you think?
I'm not sure, but issue might be in reusing spacer. Try to create new UIBarButtonSystemItemFlexibleSpace every time you need flexible space.

LeftBarButtonItem with two buttons. One Button doesnt respond to tap

So I have two buttons in the leftBarButtonItem. This is my code.
NSString *todayString = #"...";
UIBarButtonItem *todayButton = [[[UIBarButtonItem alloc] initWithTitle:todayString style:UIBarButtonItemStyleBordered target:self.calendarController action:#selector(todaySelected)] autorelease];
NSString *weeklyString = #"...";
UIBarButtonItem *weeklyButton = [[[UIBarButtonItem alloc] initWithTitle:weeklyString style:UIBarButtonItemStyleBordered target:self action:#selector(weekSelected)] autorelease];
UIToolbar *toolbar = [[[UIToolbar alloc] init] autorelease];
[toolbar setItems:[NSArray arrayWithObjects:todayButton, weeklyButton, nil]];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:toolbar] autorelease];
The title of the buttons is dynamic. Sometimes the titles can get somewhat long. The buttons expand and look fine. However, if the titles get too long, it seems that the UIToolbar reaches some width maximum, as the buttons stop responding to taps at a certain X point. See image here
Green represents responds to touch and red does not respond to touch. The Today button responds to touch. The Weekly button, however, only responds to touch until the second "e". Anything after that does not respond to touch.
I've been banging my head trying to fix this for a while, with no luck. I tried expanding the frame of both the UIToolbar and the leftBarButtonItem. Anyone know whats going on? Thanks in advance
Your toolbar frame width is probably too narrow. In your code, you don't even set its size, and rely on it to be sized it for you. Trying setting it to larger width, and make sure the autoresizingMask does not have `UIViewAutoresizingFlexibleWidth' set.
A good debugging technique is to set the background color of your new toolbar to something bright (to contrast against the toolbar you are installing it into) and watch its sizing to see how your button is being clipped interaction-wise.
You may need to explicitly resize it as you update the bar button item titles if fixing it to a certain width is too restrictive for your app.
You'll need to make a custom button view that is actionable, with something like a UIButton:
UIButton *newButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[newButton addTarget:self action:#selector(newAction:) forControlEvents:UIControlEventTouchUpInside];
[newButton setTitle:#"New Button" forState:UIControlStateNormal];
UIBarButtonItem *newBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:newButton];
self.navigationItem.leftBarButtonItem = newBarButtonItem;
for example.
EDIT - now I see what you are doing, I apologize for the earlier confusion. You are adding an array of buttons to the left side. Try adjusting the frame of the toolbar with so the buttons fit inside the interactive area:
NSString *todayString = #"...";
UIBarButtonItem *todayButton = [[UIBarButtonItem alloc] initWithTitle:todayString style:UIBarButtonItemStyleBordered target:self action:nil];
NSString *weeklyString = #"...";
UIBarButtonItem *weeklyButton = [[UIBarButtonItem alloc] initWithTitle:weeklyString style:UIBarButtonItemStyleBordered target:self action:nil];
UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar setFrame:CGRectMake(0, 0, 320, 50)];
[toolbar setItems:[NSArray arrayWithObjects:todayButton, weeklyButton, nil]];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
I know this is an old question, but it led me astray for a bit until I found updated information. I could not use the technique mentioned above in iOS 7 because the embedded toolbar would not line up with its parent.
Anyhow, iOS 5 (I believe) added button arrays for the left and right items:
UIBarButtonItem * trashItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:#selector(onTrashClick:)];
UIBarButtonItem * mailItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(onActionClick:)];
self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects: refreshBtn, selectYearBtn, nil];

How to add buttons to the top bar of a detail view from a UITableView

When an item from a UITableView is selected, a detail view is loaded which has a banner bar at the top with a back button on it to navigate back to the table.
How do I add other buttons to that banner bar?
As Andrew said, you can add custom views to the navigation bar. For example, if you are looking to add multiple buttons to the right side of the navigation bar, you can do something like this:
// right side of nav bar
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 106, 44)];
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:3];
UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
target:self
action:#selector(deleteAction:)];
deleteButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:deleteButton];
[deleteButton release];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
[spacer release];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(cancelAction:)];
cancelButton.style = UIBarButtonItemStylePlain;
[buttons addObject:cancelButton];
[cancelButton release];
[toolbar setItems:buttons animated:NO];
toolbar.barStyle = -1;
[buttons release];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
[toolbar release];
Make sure to adjust the width (106 above) if you need more or wider buttons, and then provide methods for the selectors (deleteAction: and cancelAction: above).
From your description, it sounds like you have a UINavigationController correctly set up. That "banner bar" you are describing is properly called a navigation bar.
From within the detail view, you can use:
UIBarButtonItem* button = [[[UIBarButtonItem alloc] initWithTitle:"HiMom" style:UIBarButtonItemStylePlain target:self action:#selector(onHiMom:)] autorelease];
self.navigationItem.rightBarButtonItem = button;
You can also add custom views (instead of a button) plus a few other settings. I suggest poking around the documentation for UINavigationItem, UINavigationBar, and UIBarButtonItem for ideas.

Resources