How do you programmatically set UISearchDisplayBar inside the NavigationBar for UITableViewController - ios

I currently have a table view controller and have placed a UISearchDisplayController on top or above the my table view controller inside the interface builder. Inside the viewDidLoad of my implementation file I go ahead and assign the searchbar to the navigationItem.titleView. The problem that I now have is that there is a bit more space in the first table view cell because it still thinks that the searchdisplaycontroller is there. You can see it here: http://imgur.com/u6cQew2
Here is code:
.h file
#interface searchTableViewController : UITableViewController <UISearchBarDelegate>
{
UISearchBar *searchDrugBar;
}
#property (nonatomic, strong) IBOutlet UISearchBar *searchDrugBar;
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
searchDrugBar.placeholder = #"Search for info";
self.navigationItem.titleView = self.searchDisplayController.searchBar;
[self.searchDisplayController setActive: YES animated: YES];
[self.searchDisplayController.searchBar becomeFirstResponder];
}
Any suggestions or ideas? Thanks in advance!

Try to create the searchBar programatically, I don't see any added value from creating it in the storyboard unless you have your own reason.
First remove it from the storyboard then:
- (void)viewDidLoad
{
[super viewDidLoad];
searchDrugBar = [[UISearchBar alloc] init];
searchDrugBar.frame = CGRectMake(0, 0, 200,44); // it could be unnecessary
searchDrugBar.delegate = self;
searchDrugBar.placeholder = #"Search for info";
self.navigationItem.titleView = self.searchDisplayController.searchBar;
[self.searchDisplayController setActive: YES animated: YES];
[self.searchDisplayController.searchBar becomeFirstResponder];
}

self.navigationItem.titleView = self.searchBarTop;
=> to pur searchbar left/right :-
UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
self.navigationItem.rightBarButtonItem = searchBarItem;
- (void)viewDidLoad
{
...
self.searchDisplayController.displaysSearchBarInNavigationBar = true;
self.navigationItem.leftBarButtonItem = [UIBarButtonItem new];
...
}
==> Here Given more Reference It might be Useful I hope it work for You..
How to animate add UISearchBar on top of UINavigationBar

Related

UISearchController - blank space hides status bar

I'm using a UISearchController and it is working fine. However it is leaving a blank space over the search bar (when it is focused) which partially hides the status bar. This espace
I already tried setting the self.edgesForExtendedLayout = UIRectEdgeNone also self.navigationController.extendedLayoutIncludesOpaqueBars = YES but the space is still there. I tried setting the self.definesPresentationContext = YES;but it generated that the searchBar take the status bar position like this.
I'm using a UIViewController embedded in a UINavigationController. This is the setUp that im implementing now:
#interface DirectoryViewController : UIViewController <UITableViewDataSource , UITableViewDelegate ,UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating>
#property (strong, nonatomic) NSMutableArray *personsArray;
#property (nonatomic, strong) UISearchController *searchController;
#property (nonatomic, strong) NSMutableArray *searchResults;
#implementation DirectoryViewController
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationController *searchResultsController = [[self storyboard] instantiateViewControllerWithIdentifier:#"TableSearchResultsNavController"];
// Our instance of UISearchController will use searchResults
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
// The searchcontroller's searchResultsUpdater property will contain our tableView.
self.searchController.searchResultsUpdater = self;
self.searchController.delegate = self;
self.searchController.searchBar.delegate = self;
//Cancel button tint
_searchController.searchBar.tintColor = [UIColor whiteColor];
[self.searchController.searchBar setBackgroundImage:[UIImage imageNamed:#"navBar"]
forBarPosition:0
barMetrics:UIBarMetricsDefault];
if ([self respondsToSelector:#selector(edgesForExtendedLayout)]) { /// iOS 7 or above
self.edgesForExtendedLayout = UIRectEdgeNone;
}
self.navigationController.extendedLayoutIncludesOpaqueBars = YES;
}
I appreciate any help. Thanks
I found the solution in This answer.
Changing the tableInsets:
self.directoryTable.contentInset = UIEdgeInsetsMake(-10, 0, 0, 0);
when the searchController is becameFirstResponder
and
self.directoryTable.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
when the search bar action is finished.

Subclasses of UISearchBar and UISearchController breaks VoiceOver

In my app I have a UIViewController that lets users search in a server database. (It's not filtering an existing list.) Results are displayed in a UITableView, which is the searchResultsController of the custom UISearchController.
We need a different behaviour of the 'Cancel' button so we created subclasses of UISearchController and UISearchBar.
My issue is: VoiceOver doesn't see the UITableView that display results. Instead of selecting the header, then the first row and so on, it selects the whole area and reads something about closing the view. (There's no 'Close' button). It seems like the UITableView showing the results is hidden. Here's the view hierarchy when the searchResultsController is presented.
If I initiate the searchController with the default class, I get the correct behaviour (for VoiceOver, not for the 'Cancel' button).
The documentation doesn't say anything specific about subclassing.
Any hint?
Here's the code when we initialize them:
//Showing results
self.resultsTableController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
self.resultsTableController.tableView.delegate = self;
self.resultsTableController.tableView.dataSource = self;
self.resultsTableController.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
// Configuring searchController
self.searchController = [[RedactedNameSearchController alloc] initWithSearchResultsController:self.resultsTableController];
// When initialized with UISearchController, it works:
// self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
[self.searchController.searchBar sizeToFit];
self.searchController.searchBar.delegate = self;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.delegate = self;
self.searchController.searchBar.delegate = self ;
UISearchController subclass code:
#interface MySearchController () {
UISearchBar *searchBar;
}
#end
#implementation MySearchController
- (UISearchBar *)searchBar
{
if (searchBar == nil) {
searchBar = [[MySearchBar alloc] initWithFrame:CGRectZero];
searchBar.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
}
return searchBar;
}
#end
`UISearchBar is implemented like so:
#implementation MySearchBar
-(void)setShowsCancelButton:(BOOL)showsCancelButton {
}
-(void)setShowsCancelButton:(BOOL)showsCancelButton animated:(BOOL)animated {
}
#end

self.navigationItem.hidesBackButton = YES and go back with swipe gesture on iOS7

I write the codes below in my UIViewController that uses UINavigationController.
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.hidesBackButton = YES;
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>) self;
}
I build and run my app,
self.navigationItem.hidesBackButton = YES;
above that works correctly, but
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>) self;
that one DO NOT works.
So, I re-write the code below.
- (void)viewDidLoad {
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 44.0f, 44.0f)]];
backBarButton.tintColor = [UIColor clearColor];
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
self.navigationItem.leftBarButtonItem = backBarButton;
}
It works correctly.
However, I want to use the first example.
The first one clearly express what I want to do.
Does someone have any idea?
In viewDidLoad, the view controller is not yet contained in a navigation controller, so the navigationController property is nil, which is why that line has no effect.
That said, assigning the delegate of UINavigationController's interactivePopGestureRecognizer is not good practice (I'm pretty sure it expects to be assigned to the navigation controller). Try disabling the gesture recognizer in viewWillAppear: instead:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

Two UISearchBars showing up in a view controller when only one is instantiated

I have two search bars shhowing up in a view controller. It's strange because I have the same exact code in another vc and it works fine. (the search bar in the background shouldn't be there)
here's a screenshot:
I added the delegates: <UISearchBarDelegate, UISearchDisplayDelegate>
then in the .h:
#property (nonatomic,retain) IBOutlet UISearchBar *searchBar;
in the .m:
#synthesize searchBar;
in the viewDidLoad:
self.searchBar.frame = CGRectMake(204, 11, 107,44);
self.searchBar.delegate = self;
//customize the searchbar
UITextField *searchField = [self.searchBar valueForKey:#"_searchField"];
[searchField setBackground:[UIImage imageNamed:#"search_panel.png"]];
[self.searchBar setTintColor:[UIColor redColor]];
UIImage *searchimg = [UIImage imageNamed:#"searchfield_bg.png"];
for (UIView *subview in self.searchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
UIView *bg = [[UIView alloc] initWithFrame:subview.frame];
bg.backgroundColor = [UIColor colorWithPatternImage:searchimg];
[self.searchBar insertSubview:bg aboveSubview:subview];
[subview removeFromSuperview];
break;
}
}
[self.view addSubview:self.searchBar];
and that's it. I have nothing to do with the searchBar in the Storyboard view controller xib it's added programmatically in the viewDidLoad method
thanks for any help
If you're using an outlet, your UISearchBar very likely also exists in the storyboard or the xib. Since you're also creating it in the viewDidLoad, you're making a second copy of it. Take another look at your storyboard.

UIToolbar precise size

I'm trying to customize my NavigationBar with the help of a toolbar.
I've implemented it programmatically as follows:
UIToolbar* tools = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 100, 44.01)];
and then I added it to my NavigationBar. The problem is that I have this ugly effect on the borders:
I've tried to change the y and the height values, with no results.
Do you have any ideas to avoid this?
Thanks in advance, yassa
I wouldn't do it this way.
You can achieve the same effect by adding a view with 2 buttons to the navigationItem.rightBarButtonItem. it is very simple:
// view that will hold the buttons
UIView* container = [[UIView alloc] init];
// create 1 button and add it to the container
UIButton* button = [[UIButton alloc] init........];
[container addSubview:button];
//create 2 button and add it to the container
button = [[UIButton alloc] init.........];
[container addSubview:button];
// now create a Bar button item
UIBarButtonItem* barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:container];
// set the nav bar's right button item
self.navigationItem.rightBarButtonItem = barButtonItem;
I partially agree with previous answers and comments.
The solution you suggested works fine for custom buttons. But what if I want to implement standard Edit button?
Access to the standard buttons/icons is through the UIBarButtonItem class, not UIButton. And you can't add UIBarButtonItem objects to a UIView.
After many research on the web, I've found the solution that completely cover my requirement. The toolbar must be created in the following way:
UIToolbar *tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 95.0f, 44.01f)];
tools.tintColor = self.navigationController.navigationBar.tintColor;
tools.barStyle = -1;
And this is the result:
Hope it helps!
yassa
Or you can do it in this way.
Just create new subclass of UIToolbar like this
#interface MyToolbar : UIToolbar
#end
#implementation MyToolbar
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
self.clearsContextBeforeDrawing = YES;
}
return self;
}
- (void)drawRect:(CGRect)rect {
// do nothing
}
- (void)dealloc {
[super dealloc];
}
#end
And use it as normal UIToolbar. I don't know why but it just works.

Resources