BarButtonItem Doesn't Show button after added - iphone

I want custom navigation item as facebook app.
Have two buttons and one UISearchBar. Buttons is leftBarButtonItem and rightBarButtonItem. UISearchBar is titleView.
to make searchbar in titleview become wide i removed two buttons by set it to nil as image below. and i add two buttons again when searchBarCancelButton clicked but the buttons doesn't shown.
Could u help me ?
when click searchBarCancelButton i want it to show as below
Code I have try:
func updateSearchResultsForSearchController(searchController: UISearchController) {
if navigationItem.rightBarButtonItem != nil {
self.navigationItem.rightBarButtonItem = nil
self.navigationItem.leftBarButtonItem = nil
}
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
navigationItem.setLeftBarButtonItem(getBarButtonItem("info", buttonfor: "left"), animated: false)
navigationItem.setRightBarButtonItem(getBarButtonItem("more_‌​icon", buttonfor: "right"), animated: false)
}
extension MainviewCtrl : UISearchResultsUpdating {
func updateSearchResultsForSearchController(searchController: UISearchController) {
if navigationItem.rightBarButtonItem != nil {
self.navigationItem.rightBarButtonItem = nil
self.navigationItem.leftBarButtonItem = nil
}
}
}
extension MainviewCtrl : UISearchBarDelegate {
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
navigationItem.setLeftBarButtonItem(getBarButtonItem("info", buttonfor: "left"), animated: false)
navigationItem.setRightBarButtonItem(getBarButtonItem("more_icon", buttonfor: "right"), animated: false)
}
}
class MainviewCtrl: UIViewController , UISearchControllerDelegate {
lazy var searchController : UISearchController = {
let _searchController = UISearchController(searchResultsController: nil)
_searchController.searchResultsUpdater = self
_searchController.dimsBackgroundDuringPresentation = false
_searchController.hidesNavigationBarDuringPresentation = false
_searchController.searchBar.delegate = self
_searchController.searchBar.tintColor = UIColor.whiteColor()
_searchController.searchBar.searchBarStyle = UISearchBarStyle.Minimal
var textFieldInsideSearchBar = _searchController.searchBar.valueForKey("searchField") as? UITextField
textFieldInsideSearchBar?.textColor = UIColor.whiteColor()
return _searchController
}()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.titleView = searchController.searchBar
definesPresentationContext = true
addBarButtonItems()
}
func addBarButtonItems(){
navigationItem.leftBarButtonItem = getBarButtonItem("info", buttonfor: "left")
navigationItem.rightBarButtonItem = getBarButtonItem("more_icon", buttonfor: "right")
}
func getBarButtonItem(imagename : String , buttonfor : String)-> UIBarButtonItem!{
let image = (UIImage(named: imagename))!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
switch buttonfor.lowercaseString {
case "left":
return (UIBarButtonItem(image: image, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(MainviewCtrl.infoAction)))
case "right":
return (UIBarButtonItem(image: image, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(MainviewCtrl.moreAction)))
default : break ;
}
return UIBarButtonItem()
}

updateSearchResultsForSearchController will get called again after you click cancel button and make those items nil. you can check if the search controller is active before setting them to nil in updateSearchResultsForSearchController:
func updateSearchResultsForSearchController(searchController: UISearchController) {
if navigationItem.rightBarButtonItem != nil && searchController.active {
self.navigationItem.rightBarButtonItem = nil
self.navigationItem.leftBarButtonItem = nil
}
}

Related

Swift: deallocate modally presented view controller

I have a modally presented SearchviewController that contains a UISearchController.
When swiping down it gets deallocated, but only if the searchControllers searchBar is not in editing mode. Only if I press its cancel button in advance, it gets deallocated.
How can I make sure it gets deallocated, even when in editing mode? There are definitely no strong self references within any closures...
Presenting ViewController:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
addButton()
}
func addButton() {
let mediumConfiguration = UIImage.SymbolConfiguration(scale: .large)
var checkButtonImage = UIImage(systemName: "plus", withConfiguration: mediumConfiguration)
checkButtonImage = checkButtonImage?.withTintColor(.label)
let button = UIButton(type: .contactAdd)
button.addTarget(self, action: #selector(onAddViewControllerButtonClicked(sender:)), for: .touchUpInside)
view.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
button.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
button.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
}
#objc func onAddViewControllerButtonClicked(sender: UIButton) {
let viewController = SearchViewController()
viewController.view.backgroundColor = .secondarySystemBackground
let navigationController = UINavigationController()
navigationController.viewControllers = [viewController]
self.present(navigationController, animated: true)
}
}
Presented ViewController:
class SearchViewController: UIViewController, UISearchBarDelegate, UISearchResultsUpdating {
override func viewDidLoad() {
super.viewDidLoad()
configureSearchController()
}
var searchController: UISearchController?
func configureSearchController() {
//search
searchController = UISearchController(searchResultsController: nil)
searchController?.searchResultsUpdater = self
searchController?.searchBar.delegate = self
searchController?.hidesNavigationBarDuringPresentation = false
searchController?.searchBar.searchBarStyle = .minimal
searchController?.searchBar.keyboardType = .webSearch
self.navigationItem.searchController?.searchBar.backgroundColor = .clear
self.navigationItem.searchController = searchController
self.navigationItem.hidesSearchBarWhenScrolling = false
self.definesPresentationContext = true
self.navigationItem.searchController = searchController
}
func updateSearchResults(for searchController: UISearchController) {
return
}
//check deallocation
deinit { print("\(NSStringFromClass(type(of: self))): deallocated") }
}
Can you help with that?
Thank you in advance!
Adding
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
self.navigationItem.searchController = nil
}
to SearchViewController fixes the problem for me, but admittedly I have no idea as to why this is necessary.

UISwipeGestureRecognizer causes jerkiness

I use UISwipeGestureRecogniser in my UITabBarController:
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
self.selectedIndex = Values.menuSelectedIndex
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
leftSwipe.direction = .left
rightSwipe.direction = .right
view.addGestureRecognizer(leftSwipe)
view.addGestureRecognizer(rightSwipe)
}
#objc func handleSwipes(_ sender:UISwipeGestureRecognizer) {
/*if let topController = UIApplication.topViewController() {
if (topController is HomeVC) {
if (sender.direction == .left) {
self.selectedIndex += 1
}
else if (sender.direction == .right) {
self.selectedIndex -= 1
}
}
}*/
}
}
When the topController is anything other than HomeVC, the swipe gesture should do nothing. Unfortunately, it causes jerkiness when scrolling left and right.
Edit
UIApplication.topViewController() is an extension to get the current UIViewController:
extension UIApplication {
class func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let navigationController = controller as? UINavigationController {
return topViewController(controller: navigationController.visibleViewController)
}
if let tabController = controller as? UITabBarController {
if let selected = tabController.selectedViewController {
return topViewController(controller: selected)
}
}
if let presented = controller?.presentedViewController {
return topViewController(controller: presented)
}
return controller
}
}
gestureRecognizer:shouldRecognizeSimultaniouslyWith:otherGesture would not work for me because I am using NMAMapViewDelegate and NMAMapGestureDelegate.
I got this working simply by removing the gesture whenever on a UIViewController that should not be calling handleSwipes.
In TabBarController I added:
lazy var leftSwipe: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
lazy var rightSwipe: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
public func addGestures() {
view.addGestureRecognizer(leftSwipe)
view.addGestureRecognizer(rightSwipe)
}
public func removeGestures() {
view.removeGestureRecognizer(leftSwipe)
view.removeGestureRecognizer(rightSwipe)
}
and in any UIViewControllers that should not call handleSwipes:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
(navigationController?.tabBarController as? TabBarController)?.removeGestures()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
(navigationController?.tabBarController as? TabBarController)?.addGestures()
}

UISearchController - Black Rectangle

I'm implementing a simple tableViewController with UISearchcontroller.
The problem is that every time that I press the search field then a black rectangle appear right after the keyboard shows up.
I also tried to use definesPresentationContext and searchBarStyle but it keep showing the rectangle. On the other hand, it looks like doesn't happen in the simulator since there is no keyboard.
Update: Below some photos.
ViewController:
class ListGlobalViewController: UITableViewController, StoryboardSceneBased, ViewModelBased {
static var sceneStoryboard: UIStoryboard = UIStoryboard(name: "DataSelectorViewController", bundle: Bundle.main)
// --------------------
// MARK: - Properties
// --------------------
var viewModel: ListGlobalViewModel!
private let disposeBag = DisposeBag()
private let searchController: UISearchController = {
let searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.searchBarStyle = .minimal
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = true
return searchController
}()
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView()
configure(with: viewModel)
navigationItem.largeTitleDisplayMode = .automatic
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
self.searchController.isActive = false
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
}
// --------------------
// MARK: - Functions
// --------------------
func configure(with viewModel: ListGlobalViewModel) {
self.tableView.delegate = nil
self.tableView.dataSource = nil
viewModel.outputs.listDataObservable.bind(to: self.tableView.rx.items(cellIdentifier: "listGlobalCell", cellType: ListGlobalCell.self)) { (index, model, cell) in
cell.model = model
}.disposed(by: disposeBag)
searchController.searchBar.rx.text.filterNil().throttle(1, scheduler: MainScheduler.instance).distinctUntilChanged().subscribe(viewModel.inputs.searchBarObservable).disposed(by: disposeBag)
}
}
Navigation Default Values:
let controller = UINavigationController()
controller.navigationBar.isTranslucent = false
controller.navigationBar.prefersLargeTitles = true
controller.definesPresentationContext = true
controller.navigationBar.titleTextAttributes = [ NSAttributedString.Key.font: UIFont.bold(size: 24), NSAttributedString.Key.foregroundColor: UIColor.black ]
if #available(iOS 11, *) {
controller.navigationBar.largeTitleTextAttributes = [ NSAttributedString.Key.font: UIFont.bold(size: 33), NSAttributedString.Key.foregroundColor: UIColor.black ]
}
return controller
Pictures:
Bugged
Use UITextField instead if UISearchBar. You can give any design to uitextfield. UISearch bar has limited designable attributes. Also you can reach any search function with textfield.

UINavigation controller: present and dismiss programmatically

I have a TableViewController which I want to present modally and I need it to have a NavigationBar.
To get that navbar, I have an embedded UINavigationController and as far as I know, that UINavigationController is what I have to present modally, so that's what I've done.
Everything works just fine, but I can't manage to dismiss that controller properly. Here is what I've got so far:
func presentErrorMessages(errorMessages: [String]) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Message", bundle: nil)
let infoMessagesNavigationViewController = storyBoard.instantiateViewController(withIdentifier: "InfoMessagesNavigation") as! ModalNavigationController
let infoMessagesTableViewController = infoMessagesNavigationViewController.viewControllers[0] as! InfoMessagesTableViewController
infoMessagesTableViewController.errorMessages = errorMessages
self.navigationController?.present(infoMessagesNavigationViewController, animated: true)
}
I use that to present ModalNavigationController, and this to dismiss it:
class ModalNavigationController: BaseNavigationController {
var backNavItem = UINavigationItem()
var okNavItem = UINavigationItem()
override func viewDidLoad() {
super.viewDidLoad()
let backButton = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(dismissModal))
backNavItem.leftBarButtonItem = backButton
...
var items = [UINavigationItem]()
items.append(backNavItem)
self.navigationBar.items = items
}
#objc func dismissModal() {
self.dismiss(animated: true)
}
}
When I press that back button, there is no change but the navbar which gets blank (with no title). I have the feeling that the application just 'forgets' what is the NavigationController used before the new one is presented.
How can I solve this?
Try something like this:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .done, target: self, action: #selector(dismissModal))
...
}
#objc func dismissModal() {
self.dismiss(animated: true, completion: nil)
}
I managed to solve the problem by placing and invoking the dismissfunction on my TableViewController rather than my NavigationController:
...
public func setBackButton(){
if self.navigationController != nil {
let item = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(dismissModal))
self.navigationItem.leftBarButtonItem = item
}
}
#objc func dismissModal() {
self.dismiss(animated: true)
}

UIPopoverPresentationController popover displays over the entire view

I am trying to create a rightBarButtonItem that appears throughout my app. When this barItem is clicked I want to show a modal popup using UIPopoverPresentationController. I have been able to get the button to show up on the barItem on all the views. However when i click on the button the xib takes over the entire view (including nav bar, see image below). Please see the class below:
class MyAppsNavigationController: UINavigationController, UINavigationControllerDelegate, UIPopoverPresentationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.navigationBar.barTintColor = Colors.Red01.color()
self.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]
}
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
viewController.navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "Ellipsis"), style: .plain, target: self, action: #selector(displayMenu(sender:)))
}
func displayMenu(sender: UIBarButtonItem)
{
let filterVC = DropdownMenuController(nibName: "DropdownMenuController", bundle: nil)
let nav = UINavigationController(rootViewController: filterVC)
nav.modalPresentationStyle = UIModalPresentationStyle.popover
//nav.isNavigationBarHidden = true
nav.preferredContentSize = CGSize(width: 200, height: 300)
let popover = nav.popoverPresentationController! as UIPopoverPresentationController
popover.permittedArrowDirections = .up
popover.delegate = self
popover.barButtonItem = self.navigationItem.rightBarButtonItem
popover.sourceView = self.view;
var frame:CGRect = (sender.value(forKey: "view")! as AnyObject).frame
frame.origin.y = frame.origin.y+20
popover.sourceRect = frame
popover.delegate = self
self.present(nav, animated: true, completion: nil)
}
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
}
Result when clicked on the button:
When clicked the popup takes over entire view:
Any chance you're not using the right delegate method? I think this looks better:
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
Also, in this case sourceView and sourceRect is not needed: specifying a barButtonItem for the popover presentation controller is sufficient.
https://developer.apple.com/documentation/uikit/uipopoverpresentationcontroller/1622314-barbuttonitem