Set UIBarButtonItem is not animated with iOS11 - uinavigationbar

This code worked fine before I upgraded to Swift 4 and iOS11 now the UIBarButtonItem is set but is not animated.
func setDeleteItem() {
deleteItem = UIBarButtonItem(barButtonSystemItem: .trash,
target: self,
action: nil)
self.navigationItem.setRightBarButtonItems([deleteItem], animated: true)
}
Tested with a real device and simulator with the same result.
Is it a bug?

Related

Accessory view not attached to the keyboard on iPad

I'm adding an accessory view with a "done" button on a keyboard.
I'm using this code:
let toolbar2 = UIToolbar()
toolbar2.sizeToFit()
let doneButton = UIBarButtonItem(title: "Aceptar", style: UIBarButtonItemStyle.plain, target: nil, action: #selector(donePressedPicker))
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: self, action: nil)
toolbar2.setItems([flexibleSpace, doneButton], animated: false)
alimentosText.inputAccessoryView = toolbar2
alimentosText.inputView = picker
It works fine in the simulator with all devices. I also use it on my iPhone and works fine, but when I test it on a real iPad, the accessory view, shows exactly in the middle of the screen, not over the keyboard. How can I correct this?
This is how it looks on iPad simulator:
This is how it looks on iPhone (real device):
And this is the problem running on real iPad:
Thanks

Center bar buttons in toolbar in Xcode

I have two buttons inside of a toolbar that is positioned at the bottom of the screen. I would like to center those buttons within the toolbar. Everything I have found is focused on centering buttons within a nav bar and I am unsure of how to approach this issue. What am I missing?
This is the UI I am attempting to emulate:
Adding a flexible space to the left and right of the buttons centered the buttons inside of the toolbar. Thanks to Midhun MP for the tip.
Thks #Andrew, it works
but for someone who wants it from code here is the example in Swift 5.0
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.setToolbarHidden(false, animated: true)
let spaceItemLeft = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
let nextItem = UIBarButtonItem(title: "NEXT", style: .plain, target: self, action: #selector(nextTapped))
let spaceItemRight = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
toolbarItems = [spaceItemLeft, nextItem, spaceItemRight]
}
#objc func nextTapped() {
print("NEXT Tapped")
}

My add button in my iOS 10 app is not performing an action

I started to learn Swift 3 and Xcode a week ago.
At this moment I have an small app with a navigation bar and an add button on the right, I want it to perform something but I can't figure it out...
I have this:
func criarPessoa() {
_ = pessoas.append("Ola OLA")
}
let adicionarButao = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(criarPessoa()(sender:)))
What am I doing wrong?
let adicionarButao = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(criarPessoa()(sender:)))
should be
let adicionarButao = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(NameOfTheVCTheFunctionIsIn.criarPessoa()))
You also need to add the BarButton like so :
self.navigationItem.setLeftBarButtonItems([adicionarButao], animated: false)
or:
self.navigationItem.setRightBarButtonItems([adicionarButao], animated: false)

Changing button identifier in navigtion bar bar button

I know this question has been asked before and I've found two different lines of code that purport to perform a change in the identifier for a bar button item in a navigation bar. Both compile, but neither has any effect on the identifier. I start the program with the identifier set to a Play button, and want to change it to a Pause button. I've run these two lines of code both in viewDidLoad() and inside
IBAction func startButton(sender: AnyObject) {
Here is the code. Can anyone tell me why the button identifier is not changing? And, is the rightButton/leftButton as obvious as it appears, or is there something about words in the code I don't get.
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Pause, target: self, action: "startButton:")
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "startButton"), animated: true)
Here is the entire viewDidLoad, where it does not work either.
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Pause, target: self, action: "startButton:")
}
Try below code to toggle button in UINavigationBar
In override func viewDidLoad() writes
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem:UIBarButtonSystemItem.Play, target: self, action:Selector("startButton:"))
and create two #IBAction for button
#IBAction func startButton(sender: UIButton!) {
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "stopButton:"), animated: true)
}
#IBAction func stopButton(sender: UIButton!) {
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "startButton:"), animated: true)
}
#Bendrix, you can see code which I have tried my view controller code
And video after run this code in simulator app in simulator

Changing UIToolbar Items Causes Toolbar to Go Blank

I have a custom UIToolbar created in Storyboard containing rewind, pause, and fast-forward UIBarButtonItems. I am attempting to replace the pause button with a play button when clicked. My code is as follows:
#IBOutlet weak var bottomToolbar: UIToolbar!
#IBAction func playPause() {
var newButton: UIBarButtonItem
if !self.timer.valid {
let aSelector : Selector = "updateTime"
self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: aSelector, userInfo: nil, repeats: true)
newButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "playPause")
}
else {
self.timer.invalidate()
newButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playPause")
}
var items = self.bottomToolbar.items
items?[3] = newButton
self.bottomToolbar.setItems(items, animated: true)
}
The toolbar is initialized with a dark gray background with white buttons. However, after the attempted button switch, the entire toolbar goes white. Any ideas?
UPDATE 1:
So after messing around with the colors and stepping through the code a bit more, I've found that only the pause/play button disappears in addition to the toolbar going white. But even after disappearing, clicking where it should be still sends a signal to the VC. And trying to reset the toolbar background to dark gray doesn't help.
I had a similar problem recently. What helped was setting toolbar.Items to an empty array before setting it again to your items.
My swift is very limited, so please excuse any compiler errors:
items = self.bottomToolbar.items
self.bottomToolbar.setItems:([], animated: false)
items?[3] = newButton
self.bottomToolbar.setItems(items, animated: true)

Resources