UIBarButtonItems not showing up after creating UIToolbar programmatically? - ios

I'm running across this issue which I can't seem to find the answer to online. Basically what I'm trying to do is programmatically create a UIToolbar with some UIBarButtonItems.
What I've done (as detailed below) is create the UIToolbar and then set the items of the UIToolbar to an array holding all the UIBarButtonItems I want.
Unfortunately, though the UIToolbar shows up, the UIBarButtonItems have yet to show themselves
Any suggestions or explanations as to why this is happening is much appreciated!
class DrawViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//create bar buttons
let deleteBarButton = UIBarButtonItem(image: UIImage(named: "greyDelete"), style: .Plain, target: self, action: "deleteView:")
let eraseBarButton = UIBarButtonItem(image: UIImage(named: "greyErase"), style: .Plain, target: self, action: "erase:")
let resizeBarButton = UIBarButtonItem(image: UIImage(named: "greyResize"), style: .Plain, target: self, action: "resize:")
let viewBarButton = UIBarButtonItem(image: UIImage(named: "greyView"), style: .Plain, target: self, action: "view:")
let colorBarButton = UIBarButtonItem(image: UIImage(named: "greyColor"), style: .Plain, target: self, action: "color:")
let drawBarButton = UIBarButtonItem(image: UIImage(named: "greyDraw"), style: .Plain, target: self, action: "draw:")
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: nil)
//set up toolbar
let toolbarItems = [deleteBarButton, flexibleSpace, eraseBarButton,
flexibleSpace, resizeBarButton, flexibleSpace,
viewBarButton, flexibleSpace, colorBarButton,
flexibleSpace, drawBarButton]
let toolbar = UIToolbar(frame: CGRectMake(0, view.bounds.height*0.93, view.bounds.width, view.bounds.height*0.7))
toolbar.barTintColor = UIColor(patternImage: UIImage(named: "blueToolbar")!)
toolbar.setItems(toolbarItems, animated: true)
self.view.addSubview(toolbar)
}
As you can see only the toolbar show up
And the image names (of the images used to create the UIBarButtons) are the same names as those in the Assets catalog

I would imagine the problem is the curious way you're creating the UIToolbar. This line here seems deeply suspect:
let toolbar = UIToolbar(frame: CGRectMake(0, view.bounds.height*0.93, view.bounds.width, view.bounds.height*0.7))
Why make it 0.7 times the height of your view?
To fix this problem, create the toolbar either without the frame constructor or with CGRectZero for its frame. Then use sizeToFit() to make it the appropriate size.
A simpler alternative, if possible, is to use a UINavigationController and tell it to show its own toolbar. You can then fill that by setting your view controller's toolbarItems property to your array, and it will all work without you having to create, position or manage a UIToolbar at all.

Related

large navigation bar with right bar button issue

i am setting large title to navigation bar and then on right side i am adding two buttons but button not set to the center with large title my code is as below
Code
navigationController?.navigationBar.prefersLargeTitles = true
title = "Record"
let settingImage = UIImage(named: "ic_Setting")!
let infoImage = UIImage(named: "ic_Info")!
let settingButton = UIBarButtonItem(image: settingImage, style: .plain, target: self, action: nil)
let infoButton = UIBarButtonItem(image: infoImage, style: .plain, target: self, action: nil)
navigationItem.rightBarButtonItems = [infoButton, settingButton]
i put above code inside viewDidLoad() and here is my screen shot which out put i am getting form above code and other screen shot which is my expected output
This is my output i get from above code:
I want output like this:
so please help me how to center buttons with title

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

replacing UIBarButtonItem with custom view

Found this code to insert the "Done" Button within the decimal keyboard pad and it works as long I don't use a custom view for the button like in this piece of code:
extension UITextField {
func makeKeyboardToolBar(title: String) {
let keyboardToolBar = UIToolbar()
keyboardToolBar.sizeToFit()
let flexibleSpace = UIBarButtonItem(barButtonSystemItem:
UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
let bimage = UIImageView(image: UIImage(named: "icon_plus_50"))
let doneButton = UIBarButtonItem(title: title, style: UIBarButtonItem.Style.done, target: self, action: #selector(self.doneClicked))
doneButton.customView = bimage
keyboardToolBar.setItems([flexibleSpace, doneButton], animated: true)
self.inputAccessoryView = keyboardToolBar
}
#objc func doneClicked() {
self.endEditing(true)
}
}
The image appears, but doesn't react. Do not set a custom view works instead, the "title" appears and doneClicked response as appropriate.
There are similar questions but unfort. objective-c...
Any help appreciate.
Don't create or use the UIImageView. Just create the UIBarButtonItem with the image.
let doneButton = UIBarButtonItem(image: UIImage(named: "icon_plus_50"), style: .plain, target: self, action: #selector(doneClicked))
No need to set the customView.

How can I add title and image on a bar buttonitem for a toolbar of navigation controller

How it is possible to add an image and text on a UIBarButtonItem on a navigation controller just like on modern apps such as Photos, ebay etc (see Toolbar)
I know this is possible in Interface Builder if you add a toolbar manually to a view but this is not possible if using navigation controller.
If you have a UIViewController in a UINavigationController you can set the left and the right bar button of the view controller with
// System item
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(action))
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(action))
// Tittle
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Left Button", style: .plain, target: self, action: #selector(action))
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Right Button", style: .plain, target: self, action: #selector(action))
// Image
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(action))
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(action))
// Custom view
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: view)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: view)
This is as far as I got so far. This, however only displays a text right to the image:
//Create a UIButton with an image on the left, and text to the right
var buttonImage = #imageLiteral(resourceName: "Lab")
var button : UIButton = UIButton(type: UIButtonType.custom)
button.setImage(buttonImage, for: UIControlState.normal);
button.setTitle("Caption", for: UIControlState.normal);

adding popover to programmatically added UIBarButtonItem and adding images to the same popover Swift

From past few days i am trying to generate a popover for programmatically added UIBarButtonItem but i couldn't succeed. Added to this, i even want this popover to be presented with few images sequentially which are clickable. The following is the code of how i generated a UIBarButtonItem programmatically
func imagerendering(){
let barbuttonimage = UIImage(named: "app")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let button1 = UIBarButtonItem(image: barbuttonimage, style: .Plain, target: self, action: nil)
self.navigationItem.leftBarButtonItem = button1
let attachButtonImage = UIImage(named: "icon-Attach")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let attachButton = UIBarButtonItem(image: attachButtonImage, style: UIBarButtonItemStyle.Plain, target: self, action: nil)
self.navigationItem.setRightBarButtonItems([menuButton,attachButton], animated: true)
let fixedSpace:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
fixedSpace.width = 5.0
self.navigationItem.rightBarButtonItems = [menuButton, fixedSpace, attachButton]
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName:UIFont(name: "Avenir-Black", size: 16)!]
}
I have presented my desired output in an image as a link below. please go through it and let me know whether what i am desiring is possible or not.
desired output image
To add an extra bar item on navigation bar using storyboard, you can refer these answers
After that, set a popover from storyboard(you know it well as you said) and then put required buttons with actions in your popop view. You can set image for a button instead of title text.
Look into the WYPopoverController library. It gives you the functionality you're looking for.

Resources