Add image to Navigation Bar Back Button - ios

I want to have the back button with "< [IMG]" instead of "< Back" which is by default.
let barAppearace = UIBarButtonItem.appearance()
barAppearace.setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), for:UIBarMetrics.default)
with this I have removed the text but how to add an image next to the "<".

let backButton = UIBarButtonItem(image:UIImage(named: "ic_authors_detail_back.png"), style:.Plain, target:self, action:"backButtonPressed");
backButton.tintColor = UIColor(hexString: "#ffffffff")
let leftBarButtonsArray = [backButton]
navigationItem.leftBarButtonItems = leftBarButtonsArray
Here is the code to add a button on navigation bar.Hope this helps

When you click left bar button
You can set its attribute like:
Here is option to set image.

Related

RightBarButton doesn't appear but it appears when the ViewController disappear for a second

In my app, the first ViewController appears with right navigation bar items. I want to show different bar items on right side in child VC which is appeared by pushing. The items in the first VC shows fine, but the second doesn't. The bar button shows for a second when the VC disappeared.
// The things the first VC did
navigationItem.setHidesBackButton(true, animated: true)
navigationController?.navigationBar.tintColor = .gray600
let stackView = UIStackView()
stackView.addArrangedSubviews(views: [registerButton, notificationButton])
stackView.spacing = 16
let rightBarButton = UIBarButtonItem(customView: stackView)
navigationController?.navigationBar.topItem?.rightBarButtonItem = rightBarButton
// The things the second did
navigationController?.navigationBar.tintColor = .gray600
navigationController?.navigationBar.topItem?.backButtonTitle = ""
navigationController?.navigationBar.backIndicatorImage = .back
navigationController?.navigationBar.backIndicatorTransitionMaskImage = .back
let rightBarButton = UIBarButtonItem(customView: editButton)
navigationController?.navigationBar.topItem?.rightBarButtonItem = rightBarButton
They did almost same things but the second doesn't work.
Here is the gif file i recorded. You can see Edit button for a second when the second VC disappeared. I tried to find the clues but i couldn't. Please check it and give me any comments. Thank you.
Delete the phrase navigationController?.navigationBar.topItem everywhere it appears, and never use it again, ever. It will totally break the navigation controller — as you yourself have demonstrated.
The only navigation item a view controller can talk to is its own navigationItem property. Thus the first vc is much more correct than the second vc, and so it works much better than the second vc.
you should not set a button and its title by using "topItem", instead you should set them by using navigationItem's rightBarButtonItem.
let rightBarButton = UIBarButtonItem(customView: editButton)
navigationItem.rightBarButtonItem = rightBarButton
let backButton = UIBarButtonItem(...)
navigationItem.leftBarButtonItem = backButton

How do I have navigationbar buttons in the same line as the navigation bar title?

I have two questions.
In my app's main screen, I have a navigation bar with a large title and a button,
like so. I want to have the "+" button in the same line as the navigation bar title, like in the Messages App.
I want the navigation bar in my app's detail view to look like the Wallet App's card info screen which has this style of navigation bar where the title is centred, and with the card on top.
How would I go about doing this?
I'm using SwiftUI on Xcode 11.4 with 13.4 as the target, if that matters.
For your 2nd question, You can use this below code to make navigation bar transparent:
public func makeNavigationBarWithoutBorder(color: UIColor = UIColor.clear) {
self.navigationBar.isTranslucent = true
self.navigationBar.backgroundColor = UIColor.clear
let image = UIImage.init(color: color)
self.navigationBar.setBackgroundImage(image, for: .default)
self.navigationBar.shadowImage = image
self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
}
Now for add left and right button like below.
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: Asset.greyCross.image, style: .plain, target: nil, action: nil)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: Asset.greyCross.image, style: .plain, target: nil, action: nil)
Hope this will give you the second one's solution.
I may be able to help you out, though not with SwiftUI.
It looks like you're using navigationController?.navigationBar.prefersLargeTitles = true which I don't think Facebook is using but if you wish to continue down that path with a button to the right-hand side try this.
tableView.addSubview(yourButton)
yourButton.translatesAutoresizingMaskIntoConstraints = false
yourButton.bottomAnchor.constraint(equalTo: tableView.topAnchor, constant: -10).isActive = true
yourButton.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16).isActive = true
2) The image and title are not apart of the navigation bar. It looks like a view which is constrained below it.

How to set Highlighted Adjusts Image property of Navigation bar's right bar item button

In UINavigationBar I've set right bar item image in storyboard. It's highlighted when tapped. I want to disable highlighting by setting "Highlighted Adjusts Image" to false. How to access this property?
You can use withRenderingMode(UIImage.RenderingMode.alwaysOriginal) for image to solve this:
let img = UIImage(named: "picture")!.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
let rightBarButtonItem = UIBarButtonItem(image: img, style: UIBarButtonItem.Style.plain, target: self, action: nil)
self.navigationItem.rightBarButtonItem = rightBarButtonItem

Create Default Back Button with Custom Button in LeftBarButtonItems ios

I am creating a project where I need to have a default back button and custom filter button on the left side of the navigation bar.
I have created custom back button, custom filter button and add both of them to leftBarButtonItems
navigationItem.leftBarButtonItems = alignedLeftBarButtonItems()
func alignedLeftBarButtonItems() -> [UIBarButtonItem] {
let filterButton = createLeftCustomBarButton()
// back button creation
let backButton = UIBarButtonItem(image: R.image.navBackArrow(), style: .plain, target: self, action: #selector(self.backButtonPressed(_:)))
backButton.tintColor = UIColor.white
let barButtonsItems = [
backButton,
UIBarButtonItem(customView: filterButton)
]
return barButtonsItems
}
Things are fine and working.
Concern:
Default, User can go to the previous VC by sliding from left to
right. This functionality is lost in making this. Is there a way I can
have both buttons with the sliding functionality.
You need to use UIBarButtonItem to use default left or right swipe functionality for back button:
let backButton = UIBarButtonItem(image: Image.backButton, style: UIBarButtonItemStyle.plain, target: self, action: #selector(actionBackButton))
self.navigationItem.leftBarButtonItem = backButton
With Custom button, you cannot get this functionality

Navigation Left Bar Button Item as Image Logo

I'm trying to add an image to a left on a Navigation Bar using leftBarButtonItem that will serve as a small logo. I'm able to add the image perfectly fine. My question is how do I disable it from being clickable and just keep the same state so it doesn't turn gray when someone presses it. I just want it as an image in the left of the Navigation Bar. Below is what I use to add the logo to the left of the navigation bar. Any help is greatly appreciated.
let btnName = UIButton()
btnName.setImage(UIImage(named: "NavIcon"), forState: .Normal)
btnName.frame = CGRectMake(0, 0, 40, 50)
let leftBarButton = UIBarButtonItem()
leftBarButton.customView = btnName
self.navigationItem.leftBarButtonItem = leftBarButton
btnName.userInteractionEnabled = false

Resources