How To Remove Navigation Controller Back Button Text [duplicate] - swift

This question already has answers here:
Remove text from Back button keeping the icon
(32 answers)
Closed 4 years ago.
When you create a navigation controller in the storyboard and create a segue to another view controller you automatically get this back button with an arrow and the title of previous page. (see image)
Image of problem
How can I delete the text and still have the arrow there?
or
How can I replace with my own image?
Thanks!

Inside your viewDidLoad of the setting you can use this to change the title
let backButton = UIBarButtonItem()
backButton.title = "" //in your case it will be empty or you can put the title of your choice
self.navigationController?.navigationBar.topItem?.backBarButtonItem = backButton
and this to set your own image
let backImage = UIImage(named: "backButton")
self.navigationController?.navigationBar.backIndicatorImage = backImage
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = backImage

You can implement UINavigationControllerDelegate like this:
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
let item = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
viewController.navigationItem.backBarButtonItem = item
}
it affects next pushed controller backBarButtonItemtitle.
edit:
in your ViewController
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource,UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.delegate = self
}

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)
}
}

tabbar custom button image doesn't show up

I'm trying to display a custom button for the tab bar item in my Swift project.
I added a png file, called btn_new, to the Assets folder of the Xcode project and tried to display the custom button in the custom tabbar controller class. But I can only see a circle button with the default blue color and no custom image on it in my simulator.
this is the custom tabbar controller class.
import UIKit
class CustomTabBarController: UITabBarController {
var createEventViewController: CreateEventViewController!
override func viewDidLoad() {
super.viewDidLoad()
createEventViewController = CreateEventViewController()
self.delegate = self
self.tabBar.barTintColor = UIColor.customGreen()
}
func createListNC() -> UINavigationController {
let listVC = listViewController()
listVC.tabBarItem = UITabBarItem(title: "", image: UIImage(named: "btn_new"), tag: 0)
return UINavigationController(rootViewController: listVC)
}
func setUpTabbarItems() -> [UIViewController]{
return [createListNC()]
}
}
extension CustomTabBarController: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if viewController == tabBarController.viewControllers?[0] {
let vc = CreateEventViewController()
let nc = UINavigationController(rootViewController: vc)
nc.modalPresentationStyle = .popover
self.present(nc, animated: true, completion: nil)
return false
}
return true
}
}
I double-checked the name is called exactly "btn_new", so I was not sure why the button is not displayed. Not really sure but, one thing I am concerned about is that I did not set a size for this custom icon. Can anyone tell me how can I display the button image for the tabbar item?
In your CustomTabBarController viewDidload:
let buttonImage: UIImage! = UIImage(named:
"btn_new")!.withRenderingMode(.alwaysOriginal)
(tabBar.items![0] ).selectedImage = buttonImage
Goto Storyboard-> Select tabBarItem on VC(blue selected area)

How to add label and back button to tap gesture viewcontroller in swift

I am using this pod GSImageViewerController for tap gesture.. but i am getting separate view in image with black screen.... but here i need in that black screen back button and name label.. how to add please let me know..
code for tap gesture:
import UIKit
import GSImageViewerController
class ViewController: UIViewController {
#IBOutlet weak var tapImage: UIImageView!
override func viewDidLoad()
{
super.viewDidLoad()
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
tapImage.isUserInteractionEnabled = true
tapImage.addGestureRecognizer(tapGestureRecognizer)
}
#objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
// let tappedImage = tapGestureRecognizer.view as! UIImageView
let imageInfo = GSImageInfo(image: tapImage.image!, imageMode: .aspectFit)
let transitionInfo = GSTransitionInfo(fromView: tapImage)
let imageViewer = GSImageViewerController(imageInfo: imageInfo, transitionInfo: transitionInfo)
present(imageViewer, animated: true, completion: nil)
}
}
in black image view how to add back button and label.. please suggest me
Instead of using the pod directly download GSImageViewerController.swift and add it to your project. Then you can modify the GSImageViewerController class as per your requirement.

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()

Search bar not aligned next to back button in navigation bar

I used a custom navigation controller class to remove the back button in my navigation bar, with the following:
class CustomNavigationController: UINavigationController, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
viewController.navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
}
}
In one of my VCs, I also add a search controller to the navigation bar with the following:
private func configureSearchController() {
self.searchController = UISearchController(searchResultsController: nil)
self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = true
self.navigationItem.titleView = searchController.searchBar
self.definesPresentationContext = true
}
However, it seems like the search bar is still being offset by some blank space where the title would be. I'd image the search bar should be aligned next to the back button, but there is the back button, space with blank title, then search bar.
How can I get the search bar aligned next to the back button instead of spaced because it seems there is some room for the blank title. Also, as a note, tapping on that blank space moves me back to the previous VC, although there is no actual title there.
EDIT:
View hierarchy, looks like the back button container view itself takes up a good amount of space
Try adding this in viewDidLoad
let backButton = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
backButton.setBackButtonTitlePositionAdjustment(UIOffsetMake(-1000, -1000), for: .default)
navigationItem.backBarButtonItem = backButton