I've seen some apps add a custom row to the keyboard. The row may include for example arrows, modifiers, copy and paste etc. I'd like to add one of these in my own app, but what are they really? Are they just toolbars with buttons that is hidden when the keyboard is hidden, or is there a framework/feature made especially for adding rows to the keyboard?
All is simple. You just need assign yours custom row view to UITextField's inputAccessoryView.
The most convenient way is to use UIToolBar as inputAccessoryView.
Read Custom Views for Data Input for more details.
Why need a library for it?
You can create a custom view and pin its movement to the keyboard show/hide notification to make it act like part of keyboard.
My code:
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelNumberPad)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Apply" style:UIBarButtonItemStyleDone target:self action:#selector(doneWithNumberPad)],
nil];
[numberToolbar sizeToFit];
self.textField.inputAccessoryView = numberToolbar;
Add this when textfield begins editing:
UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[doneBtn setBackgroundImage:[UIImage imageNamed:#"TextViewDone.png"] forState:UIControlStateNormal];
[doneBtn addTarget:self action:#selector(closeKeyboard) forControlEvents:UIControlEventTouchUpInside];
doneBtn.frame = CGRectMake(self.view.frame.size.width-70,6,64,30);
headerBarView = [[UIView alloc]initWithFrame:CGRectMake(0,headerY,self.view.frame.size.width,40)];
headerBarView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"TextViewBar.png"]];
[headerBarView addSubview:doneBtn];
[self.view addSubview:headerBarView];
And this when textfield ends editing:
// Action for close keyboard of header bar for UITextView *****
- (void) closeKeyboard
{
[headerBarView removeFromSuperview];
}
Related
I want to have a controller with both back button and menu button as in given image.amazon app
But the problem is the even after increasing the x-axis value it does not shift to right. Hence, I am unable to add back button image. Here's what I am doing right now.
button = [[UIButton alloc] initWithFrame:CGRectMake(200, 100, 100, 25)];
[button setImage:[UIImage imageNamed:#"menu.png"] forState:UIControlStateNormal];
[button addTarget:[SlideNavigationController sharedInstance] action:#selector(toggleLeftMenu) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[SlideNavigationController sharedInstance].leftBarButtonItem = rightBarButtonItem;
Try this,
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(back)];
UIBarButtonItem *btnMenu = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(menu)];
[self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:btnBack, btnMenu, nil]];
Well on your code you specifically replaced the back button with a barBarButtonItem.
BTW, your naming is a bit confusing you are assigning a rightBarButtonItem to the leftBarButtonItem of the navigationController.
[SlideNavigationController sharedInstance].navigationItem.leftBarButtonItems= #[leftButton,menuButton]; // this will assign both button on the left side. same thing for rightBarButtonItems
I have a UIPickerview that is created in interface builder. And via code I placed a toolbar as its subview and a done button as the toolbars bar button item. I want to execute an action when the done button is clicked (hide the pickerview and some extra stuff). But for some reason the action for the done button is not working. Here is the code to create the toolbar and the bar button item in viewDidLoad.
UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
toolBar.barTintColor = [UIColor colorWithRed:70/250.0f green:70/250.0f blue:70/250.0f alpha:1.0];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone target:self action:#selector(doneButtonTapped:)];
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
toolBar.items = #[flexible, barButtonDone, flexible];
barButtonDone.tintColor=[UIColor colorWithRed:227/255.0f green:178/255.0f blue:49/255.0f alpha:1.0];
[picker addSubview:toolBar];
[picker bringSubviewToFront:toolBar];
And here is the method doneButtonTapped:
-(void)doneButtonTapped:(UIBarButtonItem *)sender{
NSLog(#"Done button tapped");
picker.hidden = true;
[self.view bringSubviewToFront:txtCity];
[self.view bringSubviewToFront:advancedSearchLine];
[self.view bringSubviewToFront:txtCityLine];
}
I checked in layout debugger while running the app and indeed the done button is in the front of the view, so nothing in front of it to prevent it from working. What am I doing wrong?
you cannot add toolbar as subview of picker view.create a container in UIView add toolbar and picker as subview.
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, toolBar.frame.size.height + picker.frame.size.height)];
inputView.backgroundColor = [UIColor clearColor];
[inputView addSubview:picker];
[inputView addSubview:toolBar];
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.
I am using the following code to achieve a navigation bar in my app. (my app was crashing when I used a push segue so I need a modal segue meaning the nav bar is hidden after the modal segue is called)
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
//do something like background color, title, etc you self
[self.view addSubview:navbar];
Does any one know any methods I can use with the above code to achieve back button functionality in the nav bar??
Use below code to add back button on left side of navigation bar.Add UIBarButtonItem on Navigation bar.
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(backBtnClicked:)];
self.navigationItem.leftBarButtonItem = backButton;
Use below code to add back button on left side
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
//do something like background color, title, etc you self
[self.view addSubview:navbar];
UINavigationItem *item = [[UINavigationItem alloc]
init];
navbar.items= #[item];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(backBtnClicked:)];
item.leftBarButtonItem = backButton;
BackButton is better than LeftButton I think:
UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] init];
[desVC.navigationItem setBackBarButtonItem:backBtn];
Add Back Button With Image :
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 70.0f, 21.0f)];
UIImage *backImage = [UIImage imageNamed:#"backBtn"];
[backButton setImage:backImage forState:UIControlStateNormal];
[backButton setTitleEdgeInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 0.0)];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[negativeSpacer setWidth:-15];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,backButtonItem,nil];
Regarding the back button, the proper way is not to create your own.
The UINavigationItem class has proper support for this:
func setHidesBackButton(_:animated:)
var leftItemsSupplementBackButton: Bool
So, if you want to add a custom button on the left side, just set leftItemsSupplementBackButton to true.
Or call
self.navigationItem.setHidesBackButton(false, animated:false)
I have added a UIBarButtonItem to a UIToolbar, and made it emulate a UILabel as per this post.
Here is some sample code:
UIToolbar * toolBar = [[UIToolbar alloc] initWithFrame:toolbarInitialFrame];
UIBarButtonItem * labelEmu = [[UIBarButtonItem alloc] initWithTitle:#"Settings" style:UIBarButtonItemStylePlain target:self action:nil];
[labelEmu setEnabled:NO];
/* some more buttons */
[toolBar setItems:[NSArray arrayWithObjects:labelEmu, spacer, doneButton, nil]];
[container addSubview:toolBar];
My problem is that with setEnabled:NO the button is inactive but dimmed, and with setEnabled:YES the button is not dimmed, but glows when pressed.
How can I get this button to be not dimmed AND inactive (so it doesnt glow when pressed)?
Try using the UIBarButtonItem with a custom view like this:
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 25)];
[label setText:#"Settings"];
[label sizeToFit];
UIBarButtonItem * labelEmu=[[UIBarButtonItem alloc] initWithCustomView:label];