Why does the following code not open a navigation controller when pressing on the camera button? The function done specifies that the camera should push view controller but nothing happens.
func setNavigationBar() {
let screenSize: CGRect = UIScreen.main.bounds
let navBar = UINavigationBar(frame: CGRect(x: 0, y: 20, width: screenSize.width, height: 160))
let navItem = UINavigationItem(title: "Hello")
let doneItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.camera, target: nil, action: #selector(done))
navItem.rightBarButtonItem = doneItem
navBar.setItems([navItem], animated: false)
self.view.addSubview(navBar)
}
@objc func done() { // remove @objc for Swift 3
let dummyViewController = UIViewController()
navigationController?.pushViewController(dummyViewController, animated: true)
}
This is the AppDelagate:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:
[UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Thread.sleep(forTimeInterval: 2.0)
FirebaseApp.configure()
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController =
UINavigationController(rootViewController: MessagesController())
UITableViewCell.appearance().textLabel?.textColor = UIColor.white;
UITableViewCell.appearance().backgroundColor = UIColor.clear
UIApplication.shared.statusBarStyle = .lightContent
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
UINavigationBar.appearance().barTintColor = UIColor(red:0.13, green:0.15, blue:0.18, alpha:1.0)
UIApplication.shared.isStatusBarHidden = false
return true
}
func applicationWillResignActive(_ application: UIApplication) {
let im = UIImageView()
im.tag = 12
im.frame = (self.window?.frame)!
im.image = UIImage.init(named: "launchbackground.png")
application.keyWindow?.subviews.last?.addSubview(im)
}
func applicationDidEnterBackground(_ application: UIApplication) {
let im = UIImageView()
im.tag = 12
im.frame = (self.window?.frame)!
im.image = UIImage.init(named: "launchbackground.png")
application.keyWindow?.subviews.last?.addSubview(im)
}
func applicationWillEnterForeground(_ application: UIApplication) {
if let sub = application.keyWindow?.subviews.last
{
for vv in sub.subviews
{
if(vv.tag == 12)
{
vv.removeFromSuperview()
}
}
}
}
func applicationDidBecomeActive(_ application: UIApplication) {
if let sub = application.keyWindow?.subviews.last
{
for vv in sub.subviews
{
if(vv.tag == 12)
{
vv.removeFromSuperview()
}
}
}
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
Is there anything else I can add or remove to help solve this issue, any suggestions are welcome.
UPDATE:
So basically I have a root view controller that is only shown after a user is logged in. Before the user is logged in they are presented with another view controller that I am trying to build right now; this view controller is a register page that I need to have a navigation bar at the top with an option to "Login" The plan was to have the user to tap on the "Login" navigation control and be presented with navigation controller (that comes in from the right) that displays the login form.
The problem it's this line:
The target must be self not nil.
If you are just trying to put a camera button in your navigation bar to push another view controller, there's a simpler way to do that:
EDIT
So, if I understand well, you are presenting a Register Page view controller, and that’s where you need a navigation button to navigate to the next view, is that right? The problem might how you are ‘presenting’ this view controller. If you are using method present(_:animated:completion:), the view will be presented modally, which means it will not have a navigation controller nor a navigation bar.
To solve this, you can create a UINavigationController, setting your Register Page as the rootViewController, and present the Navigation Controller instead.
Try this: