In my application, I want to have a search button in the navigation bar and when the button is pressed searchbar should appear in the Navigation Bar. I've seen many posts on the subject, but somehow it does not work for me.
So, I placed a SearchDisplayController right below nav bar. then in the viewDidLoad I assign an action to the search button and remove searchbar from superview. Once button is clicked, I am calling mapSearchDisplayController.displaysSearchBarInNavigationBar = true, but nothing happends. Code extracts below:
override func viewDidLoad() {
super.viewDidLoad()
banner.delegate = self
// Add button to navbar
var filterButton : UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Organize, target: self, action: nil)
self.navigationItem.leftBarButtonItem = filterButton
var aboutButton : UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "aboutAction")
var searchButton : UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Search, target: self, action: "searchAction")
var rButtons : [UIBarButtonItem] = [aboutButton, searchButton]
self.navigationItem.rightBarButtonItems = rButtons
// Deal with Search Display Controller
self.mapSearchDisplayController.delegate = self.mapSearchDisplayController;
self.mapSearchDisplayController.searchResultsDataSource = self.mapSearchDisplayController;
self.mapSearchDisplayController.searchResultsDelegate = self.mapSearchDisplayController;
self.mapSearchDisplayController.searchBar.removeFromSuperview();
}
func searchAction(){
println("Search action")
mapSearchDisplayController.displaysSearchBarInNavigationBar = true
}
If I call displaysSearchBarInNavigationBar = true in viewDidLoad then searchBar correctly appears in the Navigation Bar.
How do I make appear in Nav bar on the button press?
Any help is appreaciated.
I created a search bar using
let searchBar = UISearchBar(frame : CGRect(0, 0, self.navigationController?.navigationBar.frame.size.width)! * 0.9, 0)
self.searchBar.placeholder = "Search"
self.searchBar.delegate = self
And then added it to the navigation bar as follows:
self.navigationItem.titleView = self.searchBar
Further styling etc can also be done but I just created and added it to the navbar on button pressed and removed it from superview when it needed to be hidden.
Related
I want customised navigation bar back button title with action.
My Image :
I need back button like this. Here i added text successfully, but not back arrow. Here my code has action for back button.
This is my code
//Custom barButtonItem with custom alert function
self.navigationItem.hidesBackButton = true
let newBackButton = UIBarButtonItem(title: "< Dialer", style: .plain, target: self, action: #selector(back(sender:)))
self.navigationItem.leftBarButtonItem = newBackButton
When i add above code it's getting like this.
func backButtonConfiguration(_ title : String, backImageName : String) {
let backImage : UIImage = UIImage(named: backImageName)!
self.navigationBar.backIndicatorImage = backImage.withRenderingMode(.alwaysTemplate)
self.navigationBar.backIndicatorTransitionMaskImage = backImage
self.navigationBar.backItem?.title = title
}
Update
if we need custom action for back button then we have to add custom BarButtonItem. And if we need bar back button look same as built in back button then we can create custom view and use it it as a bar button item
weak var customView : UIView!
lazy var leftBarButtonView : UIBarButtonItem! = {
let btnBack = UIBarButtonItem(customView: customView)
let gesture = UITapGestureRecognizer(target: self, action: #selector(btnButtonClicked(_:)))
gesture.numberOfTapsRequired = 1
self.customView.addGestureRecognizer(gesture)
self.customView.isUserInteractionEnabled = true
return btnBack
}()
#objc func btnButtonClicked(_ gesture : UITapGestureRecognizer) {
self.navigationController?.popViewController(animated: true)
}
self.navigationItem.leftBarButtonItems = [leftBarButtonView]
I am trying to code without using Storyboard or Interface Builder in my project. So, in the need of create a side menu for my app, I want to create a Navigation Bar to give the chance to open the menu by tapping on the left button of my navigation Bar.
This is what I have tried with no success:
override func viewDidLoad() {
super.viewDidLoad()
centerViewController = ViewController()
centerViewController.delegate = self
centerNavigationController = UINavigationController(rootViewController: centerViewController)
let menuButton: UIBarButtonItem = UIBarButtonItem(title: "TMDB", style: .plain, target: centerViewController, action: Selector(("toggleLeftButton")))
centerNavigationController.navigationItem.leftBarButtonItem = menuButton
view.addSubview(centerNavigationController.view)
addChildViewController(centerNavigationController)
centerNavigationController.didMove(toParentViewController: self)
}
My Navigation Bar is not showing any button nor title.
I know that is no longer necessary but still. His lessons are helpfull https://www.youtube.com/watch?v=zS-CCd4xmRY
Solved
The problem was where I was writting the code. It must be instantiated inside the viewController where the NavigationController belongs.
I mean, inside the centerViewController not in his parent as I was doing.
This lines of code:
let menuButton: UIBarButtonItem = UIBarButtonItem(title: "TMDB", style: .plain, target: self, action: Selector(("toggleLeftButton")))
self.navigationItem.leftBarButtonItem = menuButton
Inside the controller ViewController.
EDIT
Reference:
Adding UIBarButtonItem to UINav..Controller
I want to create a navigation item "Refresh" with the default XCode refresh Icon programmatically. I know how to create one with the word "Refresh" with the following code. However, I want to have the icon insetead of the word "Refresh" Thanks for helping in advance
let refreshButton = UIBarButtonItem(title: "Refresh", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(FollowVC.refresh))
Superb question.I tried sample one for your question.I got the solution and It works fine.
what you made mistake in your coding is
First your
let refreshButton = UIBarButtonItem(title: "Refresh", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(FollowVC.refresh))
is wrong.You have to use or write
UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem, target: AnyObject?, action:Selector)
Second you used UIBarButtonItemStyle.Plain.If you want refresh button you have to set UIBarButtonSystemItem.Refresh.
SWIFT
override func viewDidLoad()
{
// Create the navigation bar
let navigationBar = UINavigationBar(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: self.view.frame.size.width, height: 64))) // set frame here
//set the background color
navigationBar.backgroundColor = UIColor.white
navigationBar.delegate = self as? UINavigationBarDelegate;
// Create a navigation item with a title
let navigationItem = UINavigationItem()
navigationItem.title = "title" //If you want to set a tilte set here.Whatever you want set here.
// Create button for navigation item with refresh
let refreshButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.refresh, target: self, action: Selector(("actionRefresh:")))
// I set refreshButton for the navigation item
navigationItem.leftBarButtonItem = refreshButton
// Assign the navigation item to the navigation bar
navigationBar.items = [navigationItem]
// Make the navigation bar a subview of the current view controller
self.view.addSubview(navigationBar)
}
#pragma action method
func actionRefresh(sender: UIBarButtonItem)
{
print("The action button is refreshed")
}
Print statement is
The action button is refreshed
OBJECTIVE C
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
navbar.backgroundColor = [UIColor whiteColor];
navbar.delegate = self;
UINavigationItem * navItem = [[UINavigationItem alloc] init];
navItem.title = #"Title"; //Set Title Here Wahtever You Want Here
UIBarButtonItem *buttonRefresh = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:#selector(actionRefresh:)];
navItem.leftBarButtonItem = buttonRefresh;
navbar.items = #[navItem];
[self.view addSubview:navbar];
}
The NSLog statement is
The button action refresh is performed
See the refresh button in screenshot
Good Solutions
Add Default Icons to Navigation Bar
Navigation Bar Item Programmatically
Adding Navigation Bar item Programmatically
Instead of using .Plain, you should be using .Refresh.
let refreshButton = UIBarButtonItem(barButtonSystemItem: .Refresh, target: self, action: #selector(FollowVC.refresh))
Also you should be using the init(barButtonSystemItem:target:action:) instead of the init(title:style:target:action:).
For some reason, I have to add a UINavagationController() inside a UIViewController(), so I did the following in the view controller class:
class someViewController: UIViewController {
private let myNav = UINavigationController()
override func viewDidLoad() {
self.addChildViewController(myNav)
self.view.addSubview(myNav.view)
myNav.view.translatesAutoresizingMaskIntoConstraints = false
myNav.view.topAnchor.constraintEqualToAnchor(self.topLayoutGuide.bottomAnchor).active = true
myNav.view.leftAnchor.constraintEqualToAnchor(view.leftAnchor).active = true
myNav.view.rightAnchor.constraintEqualToAnchor(view.rightAnchor).active = true
myNav.view.heightAnchor.constraintEqualToAnchor(nil, constant: 44).active = true
myNav.didMoveToParentViewController(self)
}
}
The navigation controller is added & showing up correctly. Then I try to add a title (or Done button) to the navigation bar, however the items just don't show up. I tried a few things like this:
self.navigationItem.title = "some title"
myNav.navigationItem.title = "some title"
myNav.navigationItem.rightBarButtonItem = btnDone
What's the right way to do it in this case?
Why don't you use this simple code to add a navigation bar :
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 66)) // Offset by 20 pixels vertically to take the status bar into account
// Create a navigation item with a title
let navigationItem = UINavigationItem()
navigationItem.title = "Title"
// Create left and right button for navigation item
let leftButton = UIBarButtonItem(title: "Save", style: UIBarButtonItemStyle.Plain, target: self, action: "btn_clicked:")
let rightButton = UIBarButtonItem(title: "Right", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
// Create two buttons for the navigation item
navigationItem.leftBarButtonItem = leftButton
navigationItem.rightBarButtonItem = rightButton
// Assign the navigation item to the navigation bar
navigationBar.items = [navigationItem]
// Make the navigation bar a subview of the current view controller
self.view.addSubview(navigationBar)
I can't seem to get any buttons to appear in my navigation bar.
Flow: Navigation Controller -> Tab bar Controller -> ViewController (want to put button here)
I've tried adding it programmatically inside of ViewDidLoad:
let navigationBar = navigationController!.navigationBar
navigationBar.tintColor = UIColor.blackColor()
let leftButton = UIBarButtonItem(title: "Left Button", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
let rightButton = UIBarButtonItem(title: "Right Button", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
navigationItem.leftBarButtonItem = leftButton
navigationItem.rightBarButtonItem = rightButton
I've also tried adding it using the storyboard but it doesnt show during runtime. Any idea why I can't get a button to appear in the navigation bar
Try it like this instead:
self.tabBarController?.navigationItem.leftBarButtonItems = [leftButton]
views from storyboard or xib load after viewWillAppear method call. and viewDidLoad method call before viewWillAppear.
so navigation bar is nil during viewDidLoad method so they are not appear.so write you code in viewdidappear method and your buttons will be appeared.