Get UIViewController.title to show in modally presented UINavigationController - ios

I know this question gets asked a lot.
I've tried every answer (I think) and can't get a title to show up.
I have a static method I use to show view controllers modally.
I think I understand that a navigation controller gets its title from the title property of the view controller it is presenting, but I cannot get my vc's title to show up, and other methods I have tried aren't working, either.
static func present(vc vc: UIViewController) {
// The navCtrl presents a vc, and uses that title.
// I should be able to set this, right?
vc.title = "vc.title" // doesn't show up
vc.navigationItem.title = "vc.navigationItem.title" // nothing
vc.navigationItem.prompt = "vc.navigationItem.prompt" // doesn't show
vc.navigationController?.title = "vc.navigationController?.title" // nuttin'
let navCtrl = UINavigationController(rootViewController: vc)
navCtrl.navigationBar.tintColor = UIColor.whiteColor()
navCtrl.navigationBar.barTintColor = UIColor.blackColor()
navCtrl.navigationBar.translucent = false
// How about adding a UINavigationItem? Nope.
// let item = UINavigationItem(title: "UINavigationItem")
// navCtrl.navigationBar.pushNavigationItem(item, animated: true) // CRASH: Cannot call pushNavigationItem:animated: directly on a UINavigationBar managed by a controller.
// Can I set something on the navCtrl? Huh-uh. None of this works
navCtrl.navigationController?.title = "navCtrl.navigationController?.title"
navCtrl.navigationItem.prompt = "navCtrl.navigationItem.prompt"
navCtrl.title = "navCtrl.title"
navCtrl.navigationItem.title = "navCtrl.navigationItem.title"
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(navCtrl, animated: true, completion: nil)
}
No matter what I've tried, I just get an empty navigation bar with a close button:

Related

Presenting custom popup from popup then dismissing first popup causes flicker

Currently I create a custom popup A then when a button in it is pressed dismiss it and in the completion handler create a new popup B.
Both popups are similar in that they use a black view with alpha set to a value to simulate the grayed out screen affect that a standard alert provides.
However dismissing A before creating B causes a flicker. I would like to create B before dismissing A but haven't found a good way to do this.
Ideas on how to do this and avoid the flicker?
I'm thinking of putting the black view with the alpha set in the view controller screen that shows popup A and turning it on when popup A is shown and off when popup B is dismissed. This doesn't seem like the best solution however. Another way would be to have a single popup and show hide controls but this seems not a good solution either because it makes the storyboard VC messy.
Here's how the code looks now:
From a menu the popup A is created:
let storyBoard = UIStoryboard(name: "MenuStoryboard", bundle: nil)
let aPopup = storyBoard.instantiateViewController(withIdentifier: "Popup_A")
present(aPopup, animated: false, completion: nil)
Then from button action of Popup A:
self.dismiss(animated: false) {
let storyBoard = UIStoryboard(name: "MenuStoryboard", bundle: nil)
let bPopup = storyBoard.instantiateViewController(withIdentifier: "PopUp_B")
if let topViewController = UIApplication.shared.topMostViewController {
topViewController.present(bPopup, animated: false, completion: nil)
}
}
And the extensions:
extension UIViewController {
var topMostViewController : UIViewController {
if let presented = self.presentedViewController {
return presented.topMostViewController
}
if let navigation = self as? UINavigationController {
return navigation.visibleViewController?.topMostViewController ?? navigation
}
if let tab = self as? UITabBarController {
return tab.selectedViewController?.topMostViewController ?? tab
}
return self
}
}
extension UIApplication {
var topMostViewController : UIViewController? {
return self.keyWindow?.rootViewController?.topMostViewController
}
}
The way you do is not recommended, a View Controller should not be concerned of "dismissing itself" and then "asking" another view controller to present a new view controller.
I'd use delegation from PopupA to communicate back to whatever object presented it. That object SHOULD dismiss the PopupA view controller.
Once completed, you could try to display PopupB.

Does UISearchContainerViewController work on iOS

UISearchContainerViewController exists both on tvOS and on iOS.
Apple has sample code showing how to use it in tvOS: they configure a UISearchController, hand it to a UISearchContainerViewController, wrap that in a UINavigationController, and make it one of a UITabBarController's children.
But I have never seen an example of UISearchContainerViewController on iOS, and I can't make it work there. For example, I do exactly what Apple does, except that I push the UISearchContainerViewController onto a navigation stack, or I wrap it in a navigation controller and present it, or whatever; and there's no search field so the whole thing is useless.
Has anyone ever gotten UISearchContainerViewController to do anything useful on iOS?
I was looking for answer to the same question. But I got this working like that :
Base VC
let vc = SearchContainerVC()
let nav = UINavigationController(rootViewController: vc)
self.present(nav, animated: true, completion: nil)
SearchContainerVC
class SearchContainerVC: UISearchContainerViewController {
init() {
let searchResultsTableVC = SearchResultsTableViewController()
let searchVC = UISearchController(searchResultsController: searchResultsTableVC)
searchVC.searchBar.searchBarStyle = .minimal
searchVC.searchBar.showsCancelButton = true
searchVC.searchBar.delegate = searchResultsTableVC
searchVC.searchResultsUpdater = searchResultsTableVC
let searchBar = searchVC.searchBar
searchBar.delegate = searchResultsTableVC
searchBar.sizeToFit()
searchBar.placeholder = "Search for something"
searchVC.hidesNavigationBarDuringPresentation = false
searchVC.dimsBackgroundDuringPresentation = true
super.init(searchController: searchVC)
navigationItem.titleView = searchBar
}
}
SearchResultsTableViewController
class SearchResultsTableViewController: UITableViewController {
// I use a Table to show search items
}

Swift SideMenu show view controller

So I'm having a table view controller and using https://github.com/jonkykong/SideMenu i'm trying to display a "slide in" sidebar which works, but it doesn't show me the view that I want in the sidebar is black
// Define the menus
let menuLeftNavigationController = UISideMenuNavigationController()
menuLeftNavigationController.leftSide = true
// UISideMenuNavigationController is a subclass of UINavigationController, so do any additional configuration of it here like setting its viewControllers.
SideMenuManager.menuLeftNavigationController = menuLeftNavigationController
// Enable gestures. The left and/or right menus must be set up above for these to work.
// Note that these continue to work on the Navigation Controller independent of the View Controller it displays!
SideMenuManager.menuAddPanGestureToPresent(toView: self.navigationController!.navigationBar)
SideMenuManager.menuAddScreenEdgePanGesturesToPresent(toView: self.navigationController!.view)
When It clicks on the sidebar button I have this, which creates the animation but doesn't show the viewcontroller
func someAction(){
present(SideMenuManager.menuLeftNavigationController!, animated: true, completion: nil)
debugPrint("clicked")
}
On the repo readme, you will find your answer, take a look on the Customization
section https://github.com/jonkykong/SideMenu#sidemenumanager.
Simply set menuFadeStatusbar = false
SideMenuManager.default.menuFadeStatusBar = false
From the previous link, you will find the following information : "Draws the menuAnimationBackgroundColor behind the status bar. Default is true.
If menuFadeStatusBar is true, this color is used to fade it. Default is black."
The answer is in the comment of the snippet you posted:
// UISideMenuNavigationController is a subclass of UINavigationController,
// so do any additional configuration of it here like setting its viewControllers.
let btnMenu:UIButton = UIButton()
btnMenu.frame = CGRect(x: 20*valuePro, y: 20*valuePro, width: 40*valuePro, height: 40*valuePro)
btnMenu.backgroundColor = .red
btnMenu.addTarget(self, action: #selector(self.displayMenu), for: .touchUpInside)
self.view.addSubview(btnMenu)
#objc func displayMenu(sender: UIButton!) {
print("Button Clicked")
present(SideMenuManager.default.menuLeftNavigationController!, animated: true, completion: nil)
}
https://github.com/jonkykong/SideMenu
The SideMenu README
Code Implementation
First:
import SideMenu
From a button, do something like this:
// Define the menu
let menu = UISideMenuNavigationController(rootViewController: YourViewController)
// UISideMenuNavigationController is a subclass of UINavigationController, so do any additional configuration
// of it here like setting its viewControllers. If you're using storyboards, you'll want to do something like:
// let menu = storyboard!.instantiateViewController(withIdentifier: "RightMenu") as! UISideMenuNavigationController
present(menu, animated: true, completion: nil)
Answer
You need use UISideMenuNavigationController(rootViewController:), not UISideMenuNavigationController().
If you open the side menu from left, set leftSide to true.
let menu = UISideMenuNavigationController(rootViewController: YourViewController)
menu.leftSide = true
present(menu, animated: true, completion: nil)
Of course, you should give instance variable to UISideMenuNavigationController(rootViewController:).
I can recommend using JASidePanels
It's pretty simple and it just works. You're creating an JASidePanelController, setting this class to empty viewcontroller in your Storyboard and making this controller initial. (do not forget to import JASidePanels at the top of the class)
Then, in this class, you're implementing awakeFromNib() method like this:
leftPanel = ..//instantiating menu controller
let centerController = ...//instantiating center controller
centerPanel = UINavigationController(rootViewController: centerController)
That's it.
You can instantiate controller via their ID which can be set in Identity Inspector
let stb = UIStoryboard(name: "Main", bundle: nil) //instantiating a storyboard we will use for instantiating controllers
let someController = stb.instantiateViewController(withIdentifier: "here_your_identifier_goes") as! YourControllerClass

Navigation and Tab View controllers don't load correctly, then jump into place

When I try to present a TabViewController, I get odd behavior from both my TabBar and NavigationBar as seen in the images below. It stays as shown in the "before" image until I touch the screen or push a button. At the point it jumps to the "after" image.
Before:
After:
Code used to present the TabViewController:
let delegate = UIApplication.shared.delegate as! AppDelegate
delegate.tabViewController = TabViewController()
self.present(delegate.tabViewController!, animated: true, completion: nil)
Initialization of the TabViewController:
override func viewDidLoad() {
super.viewDidLoad()
let groupTable = GroupTableViewController()
let nav = UINavigationController(rootViewController: groupTable)
nav.title = "Groups"
nav.tabBarItem.image = UIImage(named: "groups")
let vc2 = MeViewController()
vc2.title = "Me"
vc2.tabBarItem.image = UIImage(named: "user")
// let vc3 = SettingsViewController
// vc3.title = "Settings"
// vc3.tabBarItem.image = UIImage(named: "settings")
self.viewControllers = [nav, vc2]
self.selectedIndex = 0
}
Console log, but I don't think the error is relevant:
objc[63765]: Class PLBuildVersion is implemented in both /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/AssetsLibraryServices.framework/AssetsLibraryServices (0x11916f998) and /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/PhotoLibraryServices.framework/PhotoLibraryServices (0x118069d38).
One of the two will be used. Which one is undefined.
This is a new bug I've been experiencing seemingly after updating to Xcode 8.1/MacOS Sierra.
My XCode version is Version 8.1 beta (8T47). Could this be a bug in the beta?
I'm unsure what is causing this as I didn't make a code change when this started happening.
Thanks for the help.
The viewDidLoad of the tab view controller is really too late to be configuring the tab view controller with its two child view controllers. Either do this in the "Code used to present the TabViewController", or, if you really want to do it from within the tab view controller itself, do it from the tab view controller's initializer. Then all will be well.

How to present PopOver in iOS9

I am trying to create a pop over and when I present the view controller, the background is always solid black and the size is full screen.
I can't seem to figure out what's wrong and here is the code that I have
#IBAction func distancePopOver( sender : UIBarButtonItem){
//a UIViewController that I created in the storyboard
let controller = storyboard!.instantiateViewControllerWithIdentifier("distancePopOver")
controller.modalPresentationStyle= UIModalPresentationSTyle.PopOver
controller.preferredContentSize = CGSizeMake(200,30)
self.presentViewController(controller, animated: true, completion: nil)
//Configure the Popover presentation controller
let popController = (controller.popoverPresentationController)!
popController.permittedArrowDirections = UIPopoverArrowDirection.Down
popController.barButtonItem = sender
popController.delegate = self
}
Whenever I click on the UIBarButtonItem, it presents the view in full screen, but shouldn't it be the size I specify in line 5?
Popovers are quite finicky now. First, you are going to want to configure the popoverPresentationController before you present it.
Secondly, make sure your arrow direction is pointing the way the arrow points and not where the content is respective to the UIBarButtonItem. So, if its inside UIToolbar (and is near the bottom of the screen) you'll want .Down otherwise if its a navigation bar (near the top) you'll want to use .Up.
#IBAction func distancePopOver( sender : UIBarButtonItem){
//Configure the Popover presentation controller
let popController = (controller.popoverPresentationController)!
popController.permittedArrowDirections = .Down // .Up
popController.barButtonItem = sender
popController.delegate = self
//a UIViewController that I created in the storyboard
let controller = storyboard!.instantiateViewControllerWithIdentifier("distancePopOver")
controller.modalPresentationStyle = .Popover
controller.preferredContentSize = CGSizeMake(200,30)
presentViewController(controller, animated: true, completion: nil)
}
Now if you got this far and its still not working, its because the popover's default behavior in a compact size class is to fill the screen. Since you are already setting your view controller to be the popover's delegate you'll just need to implement this delegate function: adaptivePresentationStyleForPresentationController(_:traitCollection:) and return .None for the presentation style. This will allow you to even show a real looking popover on the iPhone. See my blog post: iPhone Popover for a full example of doing this.

Resources