I'm trying to add a UIBarButtonItem by doing this function:
func setupBackButton() {
let button = UIButton(type: .custom)
button.setTitle("Done", for: .normal)
button.titleLabel?.font = .systemFont(ofSize: 40)
let barButton = UIBarButtonItem(customView: button)
//assign button to navigationbar
self.navigationItem.leftBarButtonItem = barButton
}
it works on other screens that i add it,
but on a spefic view controller it just doens't add anything.
Here is the storyboard of other screens that it works on them :
And here is the sotryboard in the screen that Doesn't work:
what might be the problem , and why i might not see the button ?
swift 4:
I added a navigation controller:
create UIBarButtonItem on viewcontroller :
override func viewDidLoad() {
super.viewDidLoad()
let leftBarButton = UIBarButtonItem(title: "Edit", style: .done, target: self, action: #selector(myLeftBarButtonItem(_:)))
self.navigationItem.leftBarButtonItem = leftBarButton
}
#objc func myLeftBarButtonItem(_ sender:UIBarButtonItem!) {
print("myLeftBarButtonItem")
}
You may try this:
if you add image in left navbar then write this :
let leftAddBarButtonItemMenu = UIBarButtonItem(image: #imageLiteral(resourceName: "menu"), style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.btnLeftMenuClicked(sender:)))
navigationItem.leftBarButtonItems = [leftAddBarButtonItemMenu]
if you add image in right navbar then write this :
let rightNavBarItem = UIBarButtonItem(image: #imageLiteral(resourceName: "menu"), style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.btnRightClicked(sender:)))
navigationItem.rightBarButtonItem = [rightNavBarItem]
If add Title in right navbar then write this:
let rightNavBarItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.btnRightClicked(sender:)))
navigationItem.rightBarButtonItem = [rightNavBarItem]
It may Helps you. Thank you
Related
Why are my top navbar buttons not showing up in Swift? the following is hoe i have added them and am not sure why they are not showing up when running the code
class TermsAndConditionsVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .blue
setBarButtons()
}
func setBarButtons() {
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(handleLogout))
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(handleLogout))
}
}
var addPostBarItem: UIBarButtonItem
if #available(iOS 13.0, *) {
addPostBarItem = UIBarButtonItem(image: .add, style: .plain, target: self, action: #selector(addPost))
} else {
addPostBarItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addPost))
// Fallback on earlier versions
}
navigationItem.rightBarButtonItems = [addPostBarItem]
I have a MainViewController embed in a Navigation Controller, as shown below:
And in MainViewController.swift, I added two UIBarButtonItem(left and right) programmatically:
class MainViewController: UIViewController {
let rightButton = UIBarButtonItem(title: "Right", style: .plain, target: self, action: #selector(onRightClick))
let leftButton = UIBarButtonItem(title: "Left", style: .plain, target: self, action: #selector(onLeftClick))
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = rightButton
navigationItem.leftBarButtonItem = leftButton
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#objc func onRightClick() {
print("[Main] Right Click")
}
#objc func onLeftClick() {
print("[Main] Left Click")
}
}
The buttons did show on the screen, but the interesting thing is, the selector functions onLeftClick and onRightClick never get called whenever I pressed left or right button. Is there anything I should do to make it works? I am using Xcode 9.3.
try with inside scope once
override func viewDidLoad() {
super.viewDidLoad()
let rightButton = UIBarButtonItem(title: "Right", style: .plain, target: self, action: #selector(self.onRightLeftClick(_ :)))
rightButton.tag = 1
let leftButton = UIBarButtonItem(title: "Left", style: .plain, target: self, action: #selector(self.onRightLeftClick(_ :)))
rightButton.tag = 2
self.navigationItem.rightBarButtonItem = rightButton
self.navigationItem.leftBarButtonItem = leftButton
}
handle the action as
func onRightLeftClick(_ sender: UIBarButtonItem){
if sender.tag == 1{
// rightButton action
}else{
// leftButton action
}
}
You can also just add the lazy keyword to the rightButton and leftButton class properties. That way, the UIBarButtonItem won't be instantiated (and the action selectors won't attempt to be resolved) until they are used inside the class. Doing it this way allows you to use the barButtonItems anywhere in the class (not just in the function they are declared in).
lazy var rightButton = UIBarButtonItem(title: "Right", style: .plain, target: self, action: #selector(onRightClick))
lazy var leftButton = UIBarButtonItem(title: "Left", style: .plain, target: self, action: #selector(onLeftClick))
I would like to do that:
Concerning the right button, I did this:
let buttonRight = UIBarButtonItem(image: UIImage(named: "back"), style: .done, target: self, action: nil)
let textRight = UIBarButtonItem(title: "Mixte", style: .done, target: self, action: nil)
self.navigationItem.rightBarButtonItems = [textRight, buttonRight]
But I can't change the size of the image, and the click on the text is different than the click on the image (clicks are separated).
I would like to have only one click for both.
And now, for the title on the middle, I really don't know how to do that.
To make both buttons execute the same method you need to add a selector.
let buttonRight = UIBarButtonItem(image: UIImage(named: "back"), style: .done, target: self, action: #selector(YourViewController.doSomething(_:))
let textRight = UIBarButtonItem(title: "Mixte", style: .done, target: self, action: #selector(YourViewController.doSomething(_:))
class YourViewController: UIViewController {
func doSomething(_ sender: AnyObject){
//
}
}
I am trying to insert a segmented control inside a navigation bar programmatically and am having some trouble loading the segmented controller in the nav bar. I am sure this very simple, but see code below. Thanks in advance!
var segmentedController: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.titleView = segmentedController
let items = ["Label A", "Label B"]
segmentedController = UISegmentedControl(items: items)
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Sign Out", style: .plain, target: self, action: #selector(handleSignOut))
navigationItem.leftBarButtonItem?.tintColor = UIColor.black
}
You should add the segmentedController to the navigation bar after initializing it!
var segmentedController: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
let items = ["Label A", "Label B"]
segmentedController = UISegmentedControl(items: items)
navigationItem.titleView = segmentedController
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Sign Out", style: .plain, target: self, action: #selector(handleSignOut))
navigationItem.leftBarButtonItem?.tintColor = UIColor.black
}
I have an array of UIToolbar items that show when an inputField becomes first responder.
//... code
toolBar.items = [
UIBarButtonItem(title: "Button 1", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(myFunction))]
myInputField.inputAccessoryView = toolBar
Is that possible to call myFunction from the above example with a parameter?
Something like...
... #selector(myFunction(someParameter)))
Similar to UIButton, parameters can be passed via the tag attribute of UIBarButtonItem.
override func viewDidLoad() {
super.viewDidLoad()
let toolBar = UIToolbar()
let firstButton = UIBarButtonItem(title: "Button 1", style: UIBarButtonItemStyle.Done, target: self, action: #selector(myFunction))
let secondButton = UIBarButtonItem(title: "Button 2", style: UIBarButtonItemStyle.Done, target: self, action: #selector(myFunction))
firstButton.tag = 1
secondButton.tag = 2
toolBar.items = [ firstButton, secondButton ]
toolBar.sizeToFit()
textField.inputAccessoryView = toolBar
}
func myFunction(sender: UIBarButtonItem) {
print("Tapped: \(sender.tag)")
}
Tapping a button prints the button's tag in this example:
Tapped: 1 // Tap button 1
Tapped: 2 // Tap button 2