Scenario
In my app there is a navigation bar with a "magnifying glass" icon in the upper righthand side. When the user taps on this icon the titleView for the navigation bar will be replaced by a UISearchBar.
I added the searchBar with this code...
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(-5.0, 0.0, 320.0, 44.0)];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 310.0, 44.0)];
searchBarView.autoresizingMask = 0;
searchBar.delegate = self;
[searchBarView addSubview:searchBar];
self.navigationItem.titleView = searchBarView;
All of this works perfectly as it should. Now I need to have the UISearchBar disappear when the user ends their editing of the UISearchBar and to be replaced with the titleView's "title" again.
Question
How do I remove the UISearchBar and display the title for the navigation controller after editing?
Try This,
searchBar.hidden = Yes;
I haven't tested it but this should work:
[self.navigationItem.titleView removeFromSuperview];
self.title = #"Nav bar title";
Related
In my view controller, I display the search controller in the navigation bar.
/*set appearance of search controller */
_searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
_searchController.searchBar.delegate = self;
_searchController.searchResultsUpdater = self;
[_searchController.searchBar sizeToFit];
_searchController.dimsBackgroundDuringPresentation = NO;
self.definesPresentationContext = YES;
_searchController.hidesNavigationBarDuringPresentation = NO;
_searchController.searchBar.searchBarStyle = UISearchBarStyleDefault;
I also created a sort button with the help of the rightBarButtonItem.
/*add drop down sort menu button to navigation bar */
[self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
self.navigationController.navigationBar.translucent = NO;
self.navigationItem.titleView = self.searchController.searchBar;
UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:#""
style:UIBarButtonItemStyleDone
target:self
action:#selector(displaySortMenu:)];
self.navigationItem.rightBarButtonItem = rightButtonItem;
rightButtonItem.image = [UIImage imageNamed:#"Sort"];
self.navigationItem.rightBarButtonItem.imageInsets = UIEdgeInsetsMake(6, 0, 0, 0);
In iOS 11, the sort button sits right next to the search controller inside the navigation bar, like so:
However, in iOS 10, the buttons moves completely outside of the navigation bar:
My first thought was to constrain the button in willLayoutSubviews, but as there is no view component of the button, I couldn't set translatesAutoresizingMaskIntoConstraints to NO. I then checked if it was an issue of placing the search controller inside the navigation bar by removing the search controller completely. Still (in iOS 10) the button stays outside of the navigation bar:
How can I fix the rightBarButtonItem to inside the navigation bar for iOS 10 and earlier ?
I needed to release the original button I created for it to correctly stay in the navigation bar as a bar button item.
[self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
self.navigationController.navigationBar.translucent = NO;
self.navigationItem.titleView = self.searchController.searchBar;
UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:#""
style:UIBarButtonItemStyleDone
target:self
action:#selector(displaySortMenu:)];
rightButtonItem.image = [UIImage imageNamed:#"Sort"];
self.navigationItem.rightBarButtonItem = rightButtonItem;
[rightButtonItem release]; // <-- this is the new line!
Following is the code to customize the searchBar embedded in the navigationBar. I set the same tintColor for both navigationBar and searchBar. Bur it seems there is some difference in tabbar's tint color and navigation bar tint color. What could be the problem?
- (void)viewdidLoad:(BOOL)animated
{
//Change the navigation bar color
[self.navigationController.navigationBar setBarTintColor:kAppNavigationItemGrayColor];
//Create a UISearchBar
UISearchBar *topBarSearchBar = [[UISearchBar alloc] init];
[topBarSearchBar setBarTintColor:kAppNavigationItemGrayColor];
//Wrapper view to hold searchbar
UIView *searchBarWrapper = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, 310.0, 44.0)];
[topBarSearchBar setFrame:CGRectMake(0.0, 0.0, 310.0, 44.0)];
[searchBarWrapper setBackgroundColor:[UIColor clearColor]];
[searchBarWrapper addSubview:self.topBarSearchBar];
[self.navigationItem setTitleView:searchBarWrapper];
self.navigationItem.hidesBackButton = YES;
}
Try this [searchBarWrapper setBackgroundColor: kAppNavigationItemGrayColor];
Try to set,
self.navigationController.navigationBar.translucent = NO;
I used UISearchBar display on UINavigationBar by setting titleView of UINavigationBar at ViewDidLoad.
UISearchBar *searchBar = [[UISearchBar alloc] init];
searchBar.searchBarStyle = UISearchBarStyleMinimal;
searchBar.tintColor = [UIColor blackColor];
searchBar.delegate = self;
searchBar.showsCancelButton = NO;
searchBar.backgroundImage = [[UIImage alloc] init];
self.navigationItem.titleView = self.searchBar;
It works, but I had a problem. When I push into NavigationController, UISearchBar delay display until view did appear. I think it seem have to animated when set into titleView. What was problem?
I have added a search bar to the titleView of my Navigation Bar.
It looks good except for when the orientation changes. For whatever reason the search bar is changing position. It's as if the search bar is popping out or is too big for the Navigation Bar all of a sudden.
Has anyone else had this problem and knows how to fix it? I am using storyboards and was wondering if there is a setting that I am missing or something. Here is my code:
-(void) viewDidAppear:(BOOL)animated
{
UIImage *addImage = [UIImage imageNamed:#"reveal-icon.png"];
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton setFrame:CGRectMake(0, 0, addImage.size.width, addImage.size.height)];
[addButton setBackgroundImage:addImage forState:UIControlStateNormal];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:addButton];
[self.navigationItem setLeftBarButtonItem:barButtonItem];
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 260.0, 44.0)];
searchBarView.autoresizingMask = 0;
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:searchBarView.frame];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
searchBar.delegate = self;
[searchBarView addSubview:searchBar];
self.navigationItem.titleView = searchBar;
}
How come this code is not producing a UINavigationBar on my UIView?
//create the view
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor whiteColor];
//everything for tabbar
UINavigationBar *navBar = [[UINavigationBar alloc] init];
//add the components to the view
[view addSubview: navBar];
//show the view
self.view = view;
I see the white view but no NavBar. Any help? What am I doing wrong?
Set the frame of navigation bar and add it as a subview of the view for it to be seen.Set a frame for any view to be positioned in its super view's coordinate system.
[navBar setFrame:CGRectMake(0,0,CGRectGetWidth(view.frame),44)];
[self.view addSubview:navBar];
Also its a navigation bar, I do not know what tab bar you are willing to see from this code you posted.