Found this code to insert the "Done" Button within the decimal keyboard pad and it works as long I don't use a custom view for the button like in this piece of code:
extension UITextField {
func makeKeyboardToolBar(title: String) {
let keyboardToolBar = UIToolbar()
keyboardToolBar.sizeToFit()
let flexibleSpace = UIBarButtonItem(barButtonSystemItem:
UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
let bimage = UIImageView(image: UIImage(named: "icon_plus_50"))
let doneButton = UIBarButtonItem(title: title, style: UIBarButtonItem.Style.done, target: self, action: #selector(self.doneClicked))
doneButton.customView = bimage
keyboardToolBar.setItems([flexibleSpace, doneButton], animated: true)
self.inputAccessoryView = keyboardToolBar
}
#objc func doneClicked() {
self.endEditing(true)
}
}
The image appears, but doesn't react. Do not set a custom view works instead, the "title" appears and doneClicked response as appropriate.
There are similar questions but unfort. objective-c...
Any help appreciate.
Don't create or use the UIImageView. Just create the UIBarButtonItem with the image.
let doneButton = UIBarButtonItem(image: UIImage(named: "icon_plus_50"), style: .plain, target: self, action: #selector(doneClicked))
No need to set the customView.
Related
Can someone clever explain me why this is happening?
I have a ViewController A and ViewController B where A does push() and B is on top.
Inside A I have created this code:
private lazy var backBarButton: UIBarButtonItem = {
let button = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: nil, action: nil)
button.tintColor = .white
return button
}()
This gives me this crazy output :) It's like default iOS arrow + mine next to each other.
At least it does not have "Back" title which is also something that I need :)
When I change my code to something like that:
private lazy var backBarButton: UIBarButtonItem = {
let button = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
button.tintColor = .white
return button
}()
I've got arrow (and can modify it's tint), no title but it's not the same as my custom image.
Why is my custom image simply not replacing the system stock one?
First I would hide the default back button using
self.navigationController?.navigationItem.hidesBackButton = true
And then I can proceed and add a new custom back button on the left items
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: nil, action: nil)
you can set custom back navigation button by below code :
private lazy var backBarButton: UIBarButtonItem = {
let button = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: nil, action: nil)
button.tintColor = .white
return button
}()
self.navigationController?.navigationItem.hidesBackButton = true
self.navigationItem.backBarButtonItem = backBarButton
I have 3 nav bar items in my navigationBar like so:
func setupNavBarButtons() {
let searchImage = UIImage(named: "search_icon")?.imageWithRenderingMode(.AlwaysOriginal)
let searchBarButtonItem = UIBarButtonItem(image: searchImage, style: .Plain, target: self, action: #selector(handleSearch))
let mapBarButtonItem = UIBarButtonItem(title: "Map", style: .Plain, target: self, action: #selector(displayMap))
navigationItem.rightBarButtonItems = [mapBarButtonItem , searchBarButtonItem]
let filterBarButtonItem = UIBarButtonItem(title: "Filter", style: .Plain , target: self, action: #selector(displayFilter))
navigationItem.leftBarButtonItem = filterBarButtonItem
}
When I tap the search icon it calls the function below:
func handleSearch() {
self.navigationItem.titleView = searchController.searchBar
}
I want to hide all navigationBarItems while user is searching and then return nav bar items once user is done searching
You can try like this in your handleSearch method remove the left and right bar button item, After that on searchBarCancelButtonClicked method of UISearchBarDelegate you can set it again that bar items.
func handleSearch() {
searchController.searchBar.hidden = false
self.navigationItem.titleView = searchController.searchBar
searchController.searchBar.becomeFirstResponder()
navigationItem.rightBarButtonItems = nil
navigationItem.leftBarButtonItems = nil
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
self.setupNavBarButtons()
searchController.searchBar.hidden = true
}
I created navigation custom class and i wanted to decorate it, I took NavDecoration.swift class and declared extension which has shown in below code, I added search bar code as well in this function, I wanted to set search bar delegate in this extension but its giving error can not assign of type 'UIViewController ' to type 'UISearchBarDelegate
extension UINavigationController {
func makeBlackNavigationbar (viewController: UIViewController, animated: Bool) {
viewController.navigationController?.navigationBar.backgroundColor? = UIColor.blackColor()
let add = UIBarButtonItem(barButtonSystemItem: .Add, target: viewController, action: "addTapped")
let play = UIBarButtonItem(title: "Play", style: .Plain, target: viewController, action: "addTapped")
viewController.navigationItem.rightBarButtonItems = [add, play]
let left = UIBarButtonItem(barButtonSystemItem: .Add, target: viewController, action: "addTapped")
viewController.navigationItem.leftBarButtonItems = [left]
let searchBar = UISearchBar(frame: CGRectZero)
searchBar.placeholder = "Search"
searchBar.delegate = viewController
viewController.navigationItem.titleView = searchBar
}}
You have to conform to UISearchBarDelegate protocol :
extension UINavigationController : UISearchBarDelegate {
...
}
**I have done by changing just few line of code **
extension UIViewController : UISearchBarDelegate {
func makeBlackNavigationbar (viewController: UIViewController, animated: Bool) {
viewController.navigationController?.navigationBar.backgroundColor? = UIColor.blackColor()
let add = UIBarButtonItem(barButtonSystemItem: .Add, target: viewController, action: "addTapped")
let play = UIBarButtonItem(title: "Play", style: .Plain, target: viewController, action: "addTapped")
viewController.navigationItem.rightBarButtonItems = [add, play]
let left = UIBarButtonItem(barButtonSystemItem: .Add, target: viewController, action: "addTapped")
viewController.navigationItem.leftBarButtonItems = [left]
let searchBar = UISearchBar(frame: CGRectZero)
searchBar.placeholder = "Search"
searchBar.delegate = viewController
viewController.navigationItem.titleView = searchBar
}}
I'm running across this issue which I can't seem to find the answer to online. Basically what I'm trying to do is programmatically create a UIToolbar with some UIBarButtonItems.
What I've done (as detailed below) is create the UIToolbar and then set the items of the UIToolbar to an array holding all the UIBarButtonItems I want.
Unfortunately, though the UIToolbar shows up, the UIBarButtonItems have yet to show themselves
Any suggestions or explanations as to why this is happening is much appreciated!
class DrawViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//create bar buttons
let deleteBarButton = UIBarButtonItem(image: UIImage(named: "greyDelete"), style: .Plain, target: self, action: "deleteView:")
let eraseBarButton = UIBarButtonItem(image: UIImage(named: "greyErase"), style: .Plain, target: self, action: "erase:")
let resizeBarButton = UIBarButtonItem(image: UIImage(named: "greyResize"), style: .Plain, target: self, action: "resize:")
let viewBarButton = UIBarButtonItem(image: UIImage(named: "greyView"), style: .Plain, target: self, action: "view:")
let colorBarButton = UIBarButtonItem(image: UIImage(named: "greyColor"), style: .Plain, target: self, action: "color:")
let drawBarButton = UIBarButtonItem(image: UIImage(named: "greyDraw"), style: .Plain, target: self, action: "draw:")
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: nil)
//set up toolbar
let toolbarItems = [deleteBarButton, flexibleSpace, eraseBarButton,
flexibleSpace, resizeBarButton, flexibleSpace,
viewBarButton, flexibleSpace, colorBarButton,
flexibleSpace, drawBarButton]
let toolbar = UIToolbar(frame: CGRectMake(0, view.bounds.height*0.93, view.bounds.width, view.bounds.height*0.7))
toolbar.barTintColor = UIColor(patternImage: UIImage(named: "blueToolbar")!)
toolbar.setItems(toolbarItems, animated: true)
self.view.addSubview(toolbar)
}
As you can see only the toolbar show up
And the image names (of the images used to create the UIBarButtons) are the same names as those in the Assets catalog
I would imagine the problem is the curious way you're creating the UIToolbar. This line here seems deeply suspect:
let toolbar = UIToolbar(frame: CGRectMake(0, view.bounds.height*0.93, view.bounds.width, view.bounds.height*0.7))
Why make it 0.7 times the height of your view?
To fix this problem, create the toolbar either without the frame constructor or with CGRectZero for its frame. Then use sizeToFit() to make it the appropriate size.
A simpler alternative, if possible, is to use a UINavigationController and tell it to show its own toolbar. You can then fill that by setting your view controller's toolbarItems property to your array, and it will all work without you having to create, position or manage a UIToolbar at all.
I'd like to add a default left bar button item to my navigation bar. It should only display when there is no back button supplied by the UINavigationController.
What is the best approach?
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if(navigationController.viewControllers.count != 1) { // not the root controller - show back button instead
return;
}
UIBarButtonItem *menuItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize
target:self
action:#selector(menuItemSelected:)];
[viewController.navigationItem setLeftBarButtonItem:menuItem];
}
Swift code to add leftBarButtonItem…
let leftMenuItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "leftMenuItemSelected:");
self.navigationItem.setLeftBarButtonItem(leftMenuItem, animated: false);
Update
For Swift 4 & above
let leftMenuItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(leftMenuItemSelected(_:)))
self.navigationItem.setLeftBarButton(leftMenuItem, animated: false);
Swift 3 version:
let done = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(YourController.done))
navigationItem.setLeftBarButtonItem(done, animated: false)
For Swift 4.x and later(5.x)
func addNavigationItemBtn(){
let backBtn = UIBarButtonItem.init(title: "Menu", style: .plain, target: self, action: #selector(menuAction))
self.navigationItem.leftBarButtonItem = backBtn
}
#objc func menuAction(){
print("menu button pressed") //Do your action here
}
Swift 4
let item = UIBarButtonItem(title: "Setting", style: .done, target: self, action: #selector(setting))
navigationItem.rightBarButtonItem = item
// then do your action in function setting
#objc
final func setting() {
print("Your work here.")
}