How to delete or hide edit UIButton for UITableViewController - ios

I add edit UIButton to UITableViewController
self.navigationItem.leftBarButtonItem = self.editButtonItem()
But I can't to find method for delete or hide it, just disabled
self.navigationItem.leftBarButtonItem?.enabled=false

You can hide the navigation's leftbarButtonItem by
self.navigationItem.leftBarButtonItem = nil;
and again set it by your method
self.navigationItem.leftBarButtonItem = self.editButtonItem()

Try doing this
self.tableView.setEditing(false, animated: true)

Related

How to change color of Navigation controller back button?

enter image description here
How can i change this button's color and its title programatically?
This is how i push my controller:
let controller = LoginController()
navigationController?.pushViewController(controller, animated: true)
inside viewDidLoad of LoginController()
please assign respective tintColor to the UIBarButtonItem
let leftBarButtonItem = UIBarButtonItem(xxx)
leftBarButtonItem.tintColor = #UIColor
navigationItem.leftBarButtonItem = leftBarButtonItem

Change action of back bar button item

I want to be able to change the action of the back bar button item on a specific UIViewController in my navigation controller so that it pops to the root view controller. I've tried the following but they don't work:
let backButton = UIBarButtonItem(title: nil, style: .plain, target: self, action: #selector(back))
self.navigationItem.backBarButtonItem = backButton
and
self.navigationItem.backBarButtonItem?.action = #selector(back)
Any suggestions?
You should use self.navigationItem.leftBarButtonItem = backButton
Good luck
First of all backBarButtonItem action not works because you can only change back button title,take a look question about it here.
Solution
In ViewController from which you want to pop to root ViewController you need to set as a delegate of UINavigationControllerDelegate
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.delegate = self
}
and implement UINavigationControllerDelegate this method`
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
if viewController.isKind(of:PreviousViewController.self) {
navigationController.popToRootViewController(animated: animated)
}
}
If my answer not fit your needs you can check similar question here.
To keep the same look and feel of the back button but change the action, see the ViewWillDisappear answer to the question regarding, "Execute action when back bar button of UINavigationController is pressed" Execute action when back bar button of UINavigationController is pressed
This is your solution, just need to set target and selector, nothing more.
private func setNavBar() {
let item = navigationItem.backBarButtonItem
item?.target = self
item?.action = #selector(self.donePressed)
navigationItem.leftBarButtonItem = item
}
#objc private func donePressed() {
self.dismiss(animated: true, completion: nil)
}

move backButton from default UINavigationbar to customNavigationbar in swift

In viewWillAppear i hide my navigationController and navigationBar like this:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(true, animated: true)
self.navigationController?.interactivePopGestureRecognizer?.delegate = nil
}
Then i added a navigationBar in my Storyboard and connected it to an IBOutlet in my UIViewController to customize it from IB.
Everything is working fine except i can not move default backButton from the original hidden navigatioBar to my new custom navigationBar.
Question:
Is there a way to move backButton from default navigationBar to this new custom navigationBar?**
Note: I don't want to add a customized back button.
You can't. You'd need to create your own UIBarButtonItem
let backItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(yourSelector))
self.navigationItem.leftBarButtonItem = backItem

Add searchBar in navigationBar after viewDidLoad?

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.

Can't add UIBarButtonItem to toolbar

After going through every single stackoverflow solution for this problem, it's still frustratingly not working for me.
//UIBarButtonItem declaration
UIBarButtonItem* button1 = [[UIBarButtonItem alloc] initWithTitle:#"Button Text"
style:UIBarButtonItemStyleBordered target:self action:#selector(myAction)];
//method 1
[self setToolbarItems:[NSArray arrayWithObjects: button1, nil] animated:YES];
//method 2
[self.navigationController.toolbar setItems:[NSArray arrayWithObject:button1]];
//method 3
self.navigationController.toolbarItems = [NSArray arrayWithObject:button1];
//displaying toolbar
[self.navigationController setToolbarHidden:NO];
None of the above methods work for displaying a button on the toolbar - all I get is a blank toolbar. Is there something obvious I'm missing here?
Move
//UIBarButtonItem declaration
UIBarButtonItem* button1 = [[UIBarButtonItem alloc] initWithTitle:#"Button Text"
style:UIBarButtonItemStyleBordered target:self action:#selector(myAction)];
//method 1
[self setToolbarItems:[NSArray arrayWithObjects: button1, nil] animated:YES];
//displaying toolbar
[self.navigationController setToolbarHidden:NO];
to viewDidAppear:(BOOL)animated this is the point where UINavigationController get toolbar items of UIViewController that it manages.
use
self.toolbarItems=[NSArray arrayWithObject:button1]
With Swift 3 / iOS 10, in the simplest case where your navigation controller will contain only one view controller, you may use the code below in order to display your view controller with a toolbar that contains a bar button item:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Show navigation controller’s built-in toolbar
navigationController?.setToolbarHidden(false, animated: false)
// Set the view controller toolbar items
let items = [UIBarButtonItem(title: "Button Text", style: .plain, target: nil, action: nil)]
setToolbarItems(items, animated: false)
}
}
However, if you plan to have several view controllers in your navigation controller's stack, you will have to call UINavigationController's setToolbarHidden(_:animated:) method in viewWillAppear() and viewWillDisappear() in order to properly show or hide the navigation controller’s built-in toolbar:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Set the view controller toolbar items
let items = [UIBarButtonItem(title: "Button Text", style: .plain, target: nil, action: nil)]
setToolbarItems(items, animated: false)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Show navigation controller’s built-in toolbar
navigationController?.setToolbarHidden(false, animated: false)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Hide navigation controller’s built-in toolbar
navigationController?.setToolbarHidden(true, animated: false)
}
}
For those looking for a Swift version, try this:
let someVC: UIViewController = ...
let someButton: UIBarButtonItem = ...
someVC.setToolbarItems([someButton], animated: true)
The UINavigationController.toolbar property documentation explicitly clarifies which API should be used for setting toolbar items:
Management of this toolbar’s contents is done through the custom view controllers associated with this navigation controller. For each view controller on the navigation stack, you can assign a custom set of toolbar items using the setToolbarItems:animated: method of UIViewController.
-- UINavigationController Class Reference

Resources