Provide a manual input method for 4.7+

The new API introduced with the Scandit Barcode Scanner 4.7 no longer contains a default manual input method that can be displayed to let the user enter the barcode through the use of the keyboard. This adjustment was made because adding your own search bar is very simple and was oftentimes necessary to nicely integrate it into the app around the scanner and customize it accordingly.

This guide will show you how to add a basic search bar to the SBSBarcodePicker. You should adjust the code from this guide to make the search bar match your own needs and your app's look and feel.

Define the properties

Add properties for the UISearchBar, a UITapGestureRecognizer and the SBSBarcodePicker (you probably already have this).

//! */

/**

Adding a UISearchBar to the SBSBarcodePicker

To add a search bar to the barcode picker you simply create it and add it to the picker's view. You can then position the search bar through layout constraints. Alternatively you can also add the search bar outside of the picker's view hierarchy (for example on top of it or above it) if that fits better for your app.

- (void)addSearchBar {
// Create the search bar and set a delegate.
self.searchBar = [[UISearchBar alloc] init];
[self.searchBar setDelegate:self];
// Add the search bar to the picker's view.
[self.scanditBarcodePicker.view addSubview:self.searchBar];
// Add layout constraints to properly place the search bar.
[self.searchBar setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.scanditBarcodePicker.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|[searchBar]|"
options:0
metrics:nil
views: @"searchBar": self.searchBar }]];
[self.scanditBarcodePicker.view addConstraint:[NSLayoutConstraint
constraintWithItem:self.searchBar
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:picker.topLayoutGuide
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0]];
// Shift the torch and camera switch buttons downwards so they are not covered by the search bar.
[self.scanditBarcodePicker.overlayController setTorchButtonLeftMargin:15 topMargin:15 + 44 width:40 height:40];
[self.scanditBarcodePicker.overlayController setCameraSwitchButtonRightMargin:15 topMargin:15 + 44 width:40 height:40];
}


If you set the search bar to display a number or decimal pad as the keyboard there will not be a search button included. A good option in that case is to add a toolbar to the keyboards accessory view that contains a button to search as shown here:

UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolbar.barStyle = UIBarStyleBlack;
toolbar.items = @[[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil],
[[UIBarButtonItem alloc]
initWithTitle:@"Search"
style:UIBarButtonItemStyleDone
target:self
action:@selector(functionToProcessText:)]];
[toolbar sizeToFit];
[self.searchBar setInputAccessoryView:toolbar];

Implement the UISearchBarDelegate

Make sure that your view controller implements the UISearchBarDelegate protocol

//! */

/**

In the searchBarTextDidBeginEditing: callback we want to make the search bar show the cancel button and add a UITapGestureRecognizer that lets us exist the search by tapping the scanner.

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchbar {
[searchbar setShowsCancelButton:YES animated:YES];
self.tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(cancelSearch)];
[self.scanditBarcodePicker.view addGestureRecognizer:self.tapRecognizer];
}
- (void)cancelSearch {
[self.searchBar resignFirstResponder];
}


In the searchBarTextDidEndEditing: callback we hide the cancel button again, make the search bar resign first responder status and remove the gesture recognizer again.

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchbar {
[searchbar setShowsCancelButton:NO animated:YES];
[searchbar resignFirstResponder];
[self.view removeGestureRecognizer:self.tapRecognizer];
self.tapRecognizer = nil;
}


When the cancel button is clicked we make the search bar resign first responder status and clear its text.

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchbar {
[searchbar resignFirstResponder];
searchbar.text = @"";
}


When the search button is clicked we make the search bar resign first responder status, hide the cancel button and can then process the search bars text.

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchbar {
[searchbar resignFirstResponder];
[searchbar setShowsCancelButton:NO animated:YES];
NSLog(@"searching for %@", searchbar.text);
}