Changing navigationItem.rightBarButton isn't resulting in visual changes - swift

For whatever reason my rightBarButton is 1. not going away when I set the rightBarButton to nil and 2. not changing when I set it to another UIBarButtonItem.
I can successfully change the title on the fly so I don't suspect the inherited navigation controller is the problem.
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "Settings"), style: .Plain, target: self, action: nil)
This simply does nothing and the right bar button remains as a button with text "Cancel".

Probably you are have some problem with image: UIImage(named: "Settings"). Check the name of the image or try to use this to fill a UIImageView
EDIT:
this the code that I used to fill a item with an image and act like a button
//set frame
button.frame = CGRectMake(0, 0, 30, 30)
button.addTarget(self, action: #selector(TorchView.showRank(_:)), forControlEvents: .TouchUpInside)
let barButton = UIBarButtonItem(customView: button)
let editButton = UIBarButtonItem(image: UIImage(named: "question"), style: .Plain, target: self, action: #selector(TorchView.tapQuestion))
//assign button to navigationbar
self.navigationItem.rightBarButtonItems = [editButton, barButton]
In this case I am using 2 rightButtons

Related

UIBarButtonItem contains shopping cart and no. of items

I have been struggling with this for hours. I am new to Swift so all I want to do is add an image of a shopping cart and a string to the .rightBarButtonItem and I cannot get it. So far I have this:
let cartIcon = UIBarButtonItem(image: UIImage(named: "shopping_cart_icon"), style: .plain, target: self, action: #selector(didTapCart(_:)))
let cartTotal = UIBarButtonItem(title: "\(cart_total)", style: .plain, target: self, action: #selector(didTapCart(_:)))
navigationItem.rightBarButtonItems = [cartIcon, cartTotal]
But with this, my icon and number is 50px apart! I also tried creating a custom UIView using this:
let view = UIView()
let button = UIButton(type: .system)
button.layer.shadowRadius = 5.0
// autolayout solution
button.translatesAutoresizingMaskIntoConstraints = false
button.widthAnchor.constraint(equalToConstant: 40).isActive = true
button.heightAnchor.constraint(equalToConstant: 40).isActive = true
button.semanticContentAttribute = .forceRightToLeft
button.setImage(UIImage(named: "shopping_cart"), for: .normal)
button.setTitle("\(cartItemArray.count)", for: .normal)
//button.addTarget(self, action: #selector(openDocuments), for: .touchUpInside)
button.sizeToFit()
view.addSubview(button)
view.frame = button.bounds
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: view)
But the icon is massive and when I shrink it, the cart total is again really far away. In both cases, the original image asset is green but it takes on the system blue tint upon building. How do I sort this out?
You are really trying to coerce a very standard iOS UIElement into being something totally different. You need to create you own button and place it where the right bar button item is at. Don't make it a barButtonItem. Just your own Button.
Here is a nice answer showing how to do so:
https://stackoverflow.com/a/31912446/793607

How to change navigation header button text in xcode project

I'm adding a button to my nav view bar like this:
let navItem = UINavigationItem(title: "Waiting Room");
let doneItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.done, target: nil, action: #selector(addTapped));
navItem.leftBarButtonItem = doneItem
mainNavBar.setItems([navItem], animated: false);
The title of the view bar changes to Waiting room when I do this,
but how do I change the text of the button itself that I'm adding
from "Done" to something else?
I'm using Swift 4.0
Try below code:
let doneItem = UIBarButtonItem(title: "Your title", style: .plain, target: self, action:#selector(addTapped));
navigationItem.leftBarButtonItem = doneItem
Hope this will help you :)

Bar Button Doesn't Appear in PageViewController

I'm learning Swift. I tried adding a button in the UIPageViewController but it didn't show when I run the application. It showed the default back button instead. I tried adding it to a UIViewController and it worked. I already added a navigational bar before inserting the BarButtonItem but it still won't show. Why is it? How do I add the button into the navigation bar of the UIPageViewController?
Expected Navigation Menu Actual Navigation Menu
if you are pushing to pageviewcontroller then try this :-
let myAttribute2 = [ NSForegroundColorAttributeName: UIColor.white
,NSFontAttributeName: UIFont(name: kFontname, size: 20.0)]
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.titleTextAttributes = myAttribute2
self.navigationController?.isNavigationBarHidden = false
self.navigationController?.navigationBar.barTintColor = kNavigationColor
let img = UIImage(named: "menu-icon")
let menuBtn = UIBarButtonItem(image: img, style: .plain, target: self, action: #selector(self.menuAction))
self.navigationItem.leftBarButtonItem = menuBtn

Left bar button item on NavigationBar not show full text when i present the viewcontroller

Left bar button with image not show full text, when i present that viewcontroller.
i use below code for it
let leftButton: UIBarButtonItem = UIBarButtonItem(image: buttonImg, style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.dismissVC))
leftButton.title = title
navigationItem.leftBarButtonItem = leftButton
And when i use backbarbuttonitem instead of leftBarButtonItem button not displayed.
please give need full suggestion

Why LeftBarButtonItem doesn't appear in the navigationbar in swift

I have two view A and view B.
From view A to view B, I do not use navigation controller, I used performSegueWithIdentifier() and I don't want to use navigation controller to pass view A to view B.
But in the view B, I want to add a leftBarButtonItem in the navigation bar(I have embed a navigation controller for view B) like a back button with a arrow.
In order to achieve 100% same with the system backbitten with arrow. I use NSMutableAttributedString and UIButton. code is below:
let string = "< Retour" as NSString
var attributedString = NSMutableAttributedString(string: string as String)
let ArrowAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont.systemFontOfSize(20)]
let TextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont.systemFontOfSize(10)]
attributedString.addAttributes(ArrowAttributes, range: string.rangeOfString("<"))
attributedString.addAttributes(ArrowAttributes, range: string.rangeOfString(" Retour"))
let backbutton = UIButton(type: .Custom)
backbutton.addTarget(self, action: "back:", forControlEvents: .TouchUpInside)
backbutton.setAttributedTitle(attributedString, forState: UIControlState.Normal)
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(customView: backbutton), animated: true)
rightBtn = UIBarButtonItem(title: "Ok", style: .Plain, target: self, action: "save:")
But I can not see left button in the navigation bar in view B…why I can not see it in the navigation bar? Why the left button disappear?
Thank you very much.
I have tried the new code for a label, but it doesn't work yet...
let barButtonBackStr = "< Back"
let attributedBarButtonBackStr = NSMutableAttributedString(string: barButtonBackStr as String)
attributedBarButtonBackStr.addAttribute(NSFontAttributeName,
value: UIFont(
name: "AmericanTypewriter-Bold",
size: 18.0)!,
range: NSRange(
location:0,
length:1))
let label = UILabel()
label.attributedText = attributedBarButtonBackStr
label.sizeToFit()
let newBackButton = UIBarButtonItem(customView: label)
self.navigationController!.navigationItem.setLeftBarButtonItem(newBackButton, animated: true)
Thank you
Try a backbutton.sizeToFit() you'll see your button
You create a button but you never set the frame
You can also set the frame directly instead of sizeToFit() method like backbutton.frame = CGRectMake(0, 0, 100, 20)
PS: TextAttributes wasn't used
You must write it to :
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
...
}
Try to set a breakpoint after your UI design and try to launch the Debug View Hierarchy to see if your button exist or is hidden by another object
Next, after you have set your button try to make:
self.navigationController!.navigationItem.leftBarButtonItem.customView.sizeToFit()