How to move UIBarButtonItem with prefersLargeTitles programmatically? - ios

I use the NavigationBar.prefersLargeTitles = true property in the name and added rightBarButtonItem. The whole block of settings of my navigationBar looks like this:
private func settingNavigationBar() {
title = "MoneyCount"
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "plus.circle"),
style: .plain,
target: self,
action: #selector(pressedPlusButton))
}
It looks like this
How do I place my rightBarButtonItem at the level of my title of NavigationBar?
I tried changing the rightBarButtonItem and NavigationBar properties, but nothing helped

Related

Why does setting a custom backBarButtonItem for a UINavigationItem result in double back buttons?

I have a very simple setup. A UINavigationController with a root UIViewController that modifies its navigation item with a custom back button item on viewDidLoad.
let backButton = UIBarButtonItem(image: backArrowImage,
style: .plain,
target: nil,
action: nil)
navigationItem.backBarButtonItem = backButton
I'm expecting this to completely replace the system back button with title and the default back arrow icon.
However when I push a new view controller on the stack, the navigation bar draws both the new custom back icon and the system back icon.
This is what I'm seeing:
This is what I would expect it to look like:
You can hide the back button
navigationItem.hidesBackButton = true
and use leftBarButtonItem for custom UIBarButtonItem
UPD
import UIKit
final class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.hidesBackButton = true
let backItem = UIBarButtonItem(image: backArrowImage, style: .plain, target: self, action: #selector(backButtonPressed))
navigationItem.leftBarButtonItem = backItem
}
#objc func backButtonPressed() {
navigationController?.popViewController(animated: true)
}
}
let backBarButtonItem: UIBarButtonItem = .init(
image: UIImage(systemName: "chevron.backward"),
style: .plain,
target: target,
action: action
)
navigationBar.topItem?.backBarButtonItem = backBarButtonItem
navigationBar.backIndicatorImage = UIImage()
navigationBar.backIndicatorTransitionMaskImage = UIImage()
This works for me, to setup custom "<" and hide the default one and still keep the backBarButtonItem behaviours
The solution was to set global UINavigationBar appearance.
Apparently this has to be done at app launch.
UINavigationBar.appearance().backIndicatorImage = backArrowImage
UINavigationBar.appearance().backIndicatorTransitionMaskImage = backArrowImage
With this approach we can preserve title animations and general back button behavior that would not be preserved if supplementing the back button with the leftBarButtonItem.

I want custom image for back button in navigation bar but got 2 back buttons

Can someone clever explain me why this is happening?
I have a ViewController A and ViewController B where A does push() and B is on top.
Inside A I have created this code:
private lazy var backBarButton: UIBarButtonItem = {
let button = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: nil, action: nil)
button.tintColor = .white
return button
}()
This gives me this crazy output :) It's like default iOS arrow + mine next to each other.
At least it does not have "Back" title which is also something that I need :)
When I change my code to something like that:
private lazy var backBarButton: UIBarButtonItem = {
let button = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
button.tintColor = .white
return button
}()
I've got arrow (and can modify it's tint), no title but it's not the same as my custom image.
Why is my custom image simply not replacing the system stock one?
First I would hide the default back button using
self.navigationController?.navigationItem.hidesBackButton = true
And then I can proceed and add a new custom back button on the left items
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: nil, action: nil)
you can set custom back navigation button by below code :
private lazy var backBarButton: UIBarButtonItem = {
let button = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: nil, action: nil)
button.tintColor = .white
return button
}()
self.navigationController?.navigationItem.hidesBackButton = true
self.navigationItem.backBarButtonItem = backBarButton

iOS 11+ Navigation Bar's buttons and title not changing on pushed view controller?

I have 2 view controllers. View Controller A** and View Controller B
I want to achieve a very simple thing. Setting left and right navigation bar buttons on View Controller A and change title and and left navigation bar button on View Controller B when B is pushed.
I design View Controller A as I wanted but I cannot change anything including title on pushed View Controller B.
Here is the code I implemented;
in viewdidLoad of A:
title = "Satışlarını Hızlandır!"
let leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "boostCloseIcon"), style: .plain, target: self, action: #selector(closeButtonTapped))
leftBarButtonItem.tintColor = .black
navigationItem.leftBarButtonItem = leftBarButtonItem
let rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "infoIcon"), style: .plain, target: self, action: #selector(infoButtonTapped))
rightBarButtonItem.tintColor = .black
navigationItem.rightBarButtonItem = rightBarButtonItem
self.navigationController?.navigationBar.barTintColor = .white
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
in viewDidLoad of B:
title = "Confirmation"
let backBarButtonItem = UIBarButtonItem(image: UIImage(named: "infoIcon"), style: .plain, target: self, action: #selector(backButtonTapped))
backBarButtonItem.tintColor = .black
navigationItem.setLeftBarButton(backBarButtonItem, animated: false)

Swift 3.0 Adding a Right Button to Navigation Bar

I have added a navigation bar to the top of a view controller. I am trying to control whether a button is visible based a condition, but I am having trouble adding the button. So far I have,
var addButton: UIBarButtonItem = UIBarButtonItem(title: "test", style: .done, target: self, action: #selector(addTapped))
override func viewDidLoad() {
super.viewDidLoad()
let boool = true
if boool {
self.navigationItem.rightBarButtonItem = self.addButton
}
else {
self.navigationItem.rightBarButtonItem = nil
}
}
func addTapped(sender: AnyObject) {
print("hjxdbsdhjbv")
}
I believe it is not working properly because I have added a navigation bar into the VC, instead of using a navigation controller and working with the bar there. I was wondering if there was a way to work with this navigation bar.
It’s simple. Put this line of code to the viewDidLoad:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "test", style: .done, target: self, action: #selector(addTapped))
Updated for Swift 4 or later:
A custom function:
#objc func action(sender: UIBarButtonItem) {
// Function body goes here
}
(Custom) Right bar button item:
self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "some_text", style: .done, target: self, action: #selector(self.action(sender:)))
(Custom) Left bar button item:
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "some_text", style: .done, target: self, action: #selector(self.action(sender:)))
Also you can add a system bar button items something like this: UIBarButtonItem.SystemItem Defines system-supplied images for bar button items: .add, .done, .cancel, .edit, .save, .compose, .reply, .organize and more.
(System) Right bar button item:
self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonItem.SystemItem.add, target: self, action: #selector(self.action(sender:)))
(System) Left bar button item:
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonItem.SystemItem.add, target: self, action: #selector(self.action(sender:)))
let rightBarButtonItem = UIBarButtonItem.init(image: UIImage(named: "EditImage"), style: .done, target: self, action: #selector(ViewController.call_Method))
self.navigationItem.rightBarButtonItem = rightBarButtonItem
You say you added a UINavigationBar to your view controller via storyboard, but looking at the code you provided there is no outlet connection to your navigation bar in IB.
In order to access self.navigationItem your view controller must be embedded in a UINavigationController or be part of a hierarchy which is. Unless you have a need for a custom navigation bar on an individual view controller, I suggest removing that from Interface Builder, then making sure either the view controller in question is embedded in a UINavigationController or it is being pushed onto the navigation stack from another controller which is embedded in a navigation controller and then you should see your UIBarButtonItem.
Firstly, you need to connect you navigation bar to an IBOutlet so that you can refer to it in your code. After that, this code should work:
let navigationItem = UINavigationItem(title: "Title")
navigationItem.rightBarButtonItem = self.addButton
navigationItem.hidesBackButton = true
self.navigationBar.pushItem(navigationItem, animated: false)
Swift 4.2;
Add viewController
override func viewDidLoad() {
super.viewDidLoad()
self.addNavigationBarButton(imageName: "ic_back", direction:.left)
}
Add Class your API or Utility Class
public func addNavigationBarButton(imageName:String,direction:direction){
var image = UIImage(named: imageName)
image = image?.withRenderingMode(.alwaysOriginal)
switch direction {
case .left:
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style:.plain, target: nil, action: #selector(goBack))
case .right:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style:.plain, target: nil, action: #selector(goBack))
}
}
#objc public func goBack() {
self.navigationController?.popViewController(animated: true)
}
public enum direction {
case right
case left
}
tested in Xcode 10.2, swift 5.0;
First, I have have embedded my ViewController in UINavigationController in IB.
Then in ViewDidLoad include these lines
self.title = "orange"
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(changeLayout)).
Note - accessing title, or adding button through navigation controller did not work. For example : setting title - Self.navigationcontroller.navigationItem.title did not work ,

iOS Navigation Items - toggle programmatically

I am new to iOS (swift) programming. I am creating my first stop watch application. I have added a navigationBar to the MainStoryBoard, and then two navigation items on the bar, one on the left and one on the right. I would like to change the text of left navigation item in the viewDidLoad method and then in an IBAction. Can anyone help me please.
With my .NET coding intellect, I created IBOutlets for each of them and then tried changing their title properties but have failed.
Another similar question regarding the same is how can I toggle the icons on navigation items programmatically.
For UIBarButtonItem with custom button image:
let btnName = UIButton()
btnName.setImage(UIImage(named: "imagename"), forState: .Normal)
btnName.frame = CGRectMake(0, 0, 30, 30)
btnName.addTarget(self, action: Selector("action"), forControlEvents: .TouchUpInside)
Set Right/Left Bar Button item:
let rightBarButton = UIBarButtonItem()
rightBarButton.customView = btnName
self.navigationItem.rightBarButtonItem = rightBarButton
For System UIBarButtonItem:
let camera = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: Selector("btnOpenCamera"))
self.navigationItem.rightBarButtonItem = camera
You should don't create UINavigationBar on StoryBoard. You should ember it into UINavigationController. And in viewDidLoad you can add NavigationItem programmatically.
Add NavigationBarItem just with text:
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "left", style: .Done, target: self, action: "leftTap")
With Image:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "backIcon#3x"), style: .Done, target: self, action: "rightTap")
And in Action you can change text or image:
self.navigationItem.leftBarButtonItem?.title = "left change"
self.navigationItem.rightBarButtonItem?.image = UIImage(named: "calendar-icons#3x")
With title and image above just is demo. You can reference to my demo code: Demo Change NavigationItem Programmatically
Hop this help!

Resources