Navigation Bar Button Not Visible - swift

This is my modified AppDelegate func
var window: UIWindow
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let button = UIBarButtonItem(title: "Logs", style: .plain, target: self, action: #selector(logButtonTapped))
window?.rootViewController?.navigationItem.rightBarButtonItem = button
return true
}
I also added Navigation Controller before my VC as below
Even if the title is visible, the right navigation button is not visible. How can I make the navigation button visible so that I can click on it?
Plus, I get this error on console
UINavigationBar decoded as unlocked for UINavigationController, or navigationBar delegate set up incorrectly. Inconsistent configuration may cause problems.

I am not sure if I understand what you are expecting very well. But I think you need to see the navBar button on the first page of your application. Isn't it?
So instead of doing this in the AppDelegate or SceneDelegate, you can add it directly to your viewController in the viewDidLoad() method.
It will be something like this:
override func viewDidLoad() {
super.viewDidLoad()
let button = UIBarButtonItem(title: "Logs", style: .plain, target: self, action: #selector(self.logButtonTapped))
navigationItem.rightBarButtonItem = button
}
If this is not what you need, please provide more info about this

Related

Open swiftUI view from UIKit code for rightBarItem

This is code of UIKit File.I want to open view which is editButtonCustomizeView and written in swiftUI. How can I take action of this button which navigates to swiftUI
override func viewDidLoad() { super.viewDidLoad()
let editButton = UIBarButtonItem(image: editImage, style: .Plain,
target: self, action: editButtonCustomizeView())
}
Right now getting error is "Cannot convert value of type 'editButtonCustomizeView' to expected argument type 'Selector?' "
That is because the action expects a selector to point to what action is to be called when the button is tapped so you can't pass a view.
If you want to show a SwiftUI view when that button is tapped you have to first add a action to show the viewcontroller, then create the action to show the viewcontroller and lastly add you SwiftUI view in a viewcontroller so it can be used in UIKit.
Adding the action to the button:
let editButton = UIBarButtonItem(image: editImage, style: .Plain, target: self, action: #selector(showSwiftUIView))
Then the action to show the view:
#objc func showSwiftUIView(sender: UIControl) {
let vc = editButtonCustomizeVC()
present(vc, animated: true)
}
And lastly turn your view to a ViewController:
final class editButtonCustomizeVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let childView = UIHostingController(rootView: editButtonCustomizeView())
addChild(childView)
childView.view.frame = view.bounds
view.addSubview(childView.view)
childView.didMove(toParent: self)
}
}

Can't push a view controller again after popping it

I want to push a view controller onto the screen when a button is pressed. It works fine the first time, but when I go back and press the same button once again, it does not push the view controller again. The same problem persists when I present the view controller in fullscreen mode. But I don't face this problem when presenting the view controller in the default mode, I am able to present and dismiss the view controller as many times as I want. Someone please help me out with my problem.
[![enter image description here][3]][3]
[![enter image description here][4]][4]
[![enter image description here][5]][5]
import UIKit
#main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = UINavigationController(rootViewController: LoginViewController())
window.makeKeyAndVisible()
self.window = window
return true
}
}
#objc func signUpButtonPressed() {
let vc = SignUpViewController()
self.navigationController?.pushViewController(vc, animated: true)
}
#objc func loginButtonPressed() {
self.navigationController?.popViewController(animated: true)
}
func configureNavigationBar() {
navigationController?.navigationBar.backgroundColor = .systemBackground
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(createButtonPressed))
title = "Sign Up"
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont(name: Extensions.standardFont, size: 25)!]
navigationItem.rightBarButtonItem?.tintColor = .systemBlue
}
Please ignore the scrollview in the Sign up window, it is a work in progress and does nothing at this point in time. The Login, done and create account buttons all do the same work which is to pop the signup view controller. I am able to push the signup view controller once and pop it. But if I try to press the sign up button again, the button wouldn't even respond.
Try this in you objc func to navigate to next view controller:
#objc func signUpButtonPressedTapped() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "SignUpViewController") as! SignUpViewController
self.navigationController?.pushViewController(vc, animated: true)
}
}

How to set title of navigation item after open another view controller using with present modally - Swift

I have two view controllers in my application. Root view controller has some components and one of them is a button. That button provides to open another view controller using with present function. Second view controller(SelectTimeViewController) that opens when the button is tapped, was opened successfully. I am trying to set navigation title and items but I can not see them.
I did not use storyboard so root view controller is setting from AppDelegate.
let viewController = ViewController()
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = UINavigationController(rootViewController: viewController)
window?.makeKeyAndVisible()
When tapped the button, "openVC" function is invoked.
#IBAction func openVC(_ sender: Any) {
self.navigationController?.present(SelectTimeViewController(), animated: true, completion: nil)
}
I am trying to set title and rightBarButtonItem in SelectTimeViewController's viewDidLoad function but I can not see both of them.
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "close"), style: .plain, target: self, action: #selector(closeIt))
self.navigationItem.title = "Select Time"
}
In additional to this, I can see both title and right bar button item when change the "openVC" function like as bellow.
#IBAction func openVC(_ sender: Any) {
self.navigationController?.pushViewController(vc, animated: true)
}
You have to push the SelectTimeViewController instead of present
Or if you really want to present it, you should present another UINavigationController that has the rootViewController as SelectTimeViewController()

Trying to add buttons to my table view Navigation Bar

I am trying to add a button to go back to a previous view controller. Since it is a table view Controller I have attempted to add a Navigation Controller hoping that I can put a button in there.
I have also tried doing it programmatically:
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(handleCancel))
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(handleCancel))
fetchUser()
}
#objc func handleCancel(){
dismiss(animated: true, completion: nil)
}
Here is a picture of me attempting to set the navigation bar
link for image
I am not able to see the navigation item or the bar or anything. it is just the table showing up. anyone Knows how to actually get it to work?
I'm a little late but:
go to Main.storyboard -> in the top bar go to editor -> embed in -> navigation controller. do that to your viewController and than when u go to any other viewController from that ViewController you will be having that back button

Back Buttons from navigation bar are overlapping , swift 4

I was trying to customize my back button according to this tutorial.
In AppDelegate,
let barButtonAppearence = UIBarButtonItem.appearance()
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
let backButton = UIImage(named: "back_arrow")
let backButtonImage = backButton?.stretchableImage(withLeftCapWidth: 0, topCapHeight: 10)
barButtonAppearence.setBackButtonBackgroundImage(backButtonImage, for: .normal, barMetrics: .default)
return true
}
And then it is conflicting with the existing one (which appeared automatically because of segue(Show).
So I need to remove the blue one.
There are two things need to accomplish what you are trying to achieve:
Changing the back button's default image to your supplied image
Removing the title from the back button item
To change back button's image:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Remember, this will change every navigation bar's back button image in your app
UINavigationBar.appearance().backIndicatorImage = #imageLiteral(resourceName: "backButton")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = #imageLiteral(resourceName: "backButton")
return true
}
Note: If you don't want that all your navigation bar back button to have the supplied image, you may need to subclass UINavigationController and update its navigation bar.
Removing the title from the back button item:
We will do this by adding a method to any UIViewController through extension. Extension method will be used here so that any UIViewController can be able to have this behavior when it wants.
extension UIViewController {
func removeNavigationBarBackButtonItemTitle() {
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItem.Style.plain, target: nil, action: nil)
}
}
Now, in any push transition for VC A -> VC B you need to hide the back button's title. But you have to invoke the method removeNavigationBarBackButtonItemTitle() from VC A. Just remember this and you are good to go.
class ViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
removeNavigationBarBackButtonItemTitle()
}
}
You will find a demo here. The demo also has some other implementations too. But you will get what you need and what I've said above.
Have you tried replacing it this way:
UINavigationBar.appearance().backIndicatorImage = UIImage(named: "back_arrow")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "back_arrow")
I have tried this on applicationDidFinishLaunching method in AppDelegate
Another alternative solution to set tint color clear. See the below-modified code of your.
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
let backButton = UIImage(named: "back_arrow")
let backButtonImage = backButton?.stretchableImage(withLeftCapWidth: 0, topCapHeight: 10)
barButtonAppearence.setBackButtonBackgroundImage(backButtonImage, for: .normal, barMetrics: .default)
barButtonAppearence.tintColor = UIColor.clear
return true
}