Set constraint of bar button item Swift? - swift

I'm programmatically adding a bar button item like this:
let button = UIButton.init(type: .custom)
button.setImage(UIImage(named: "Bar Chart Filled-50"), for: UIControlState.normal)
button.addTarget(self, action: #selector(self.statButtonPressed), for: UIControlEvents.touchUpInside)
button.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
let barButton = UIBarButtonItem(customView: button)
navItem.rightBarButtonItem = barButton
I want it to be vertically centered in the page's navigation bar. How would I do such a thing programatically.
image:

Related

Make text as UINavigationController back button

I need to make this kind of navigation bar
But I don't know how can I set backBarButton as text. Should I create xib that will replace my navigation bar or I can do it programmatically
In viewDidLoad set your custom button navBar:
let button = UIButton(type: .custom)
//Set the image
button.setImage(UIImage(systemName: "chevron.backward"), for: .normal)
//Set the title
button.setTitle("Yourtitle", for: .normal)
//Add target
button.addTarget(self, action: #selector(callMethod), for: .touchUpInside) //call button tap action
// Add insets padding
let spacing: CGFloat = -10 // the amount of spacing to appear between image and title
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: spacing, bottom: 0, right: 0)
button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) // customize your button titleEdgeInsets if you want
//Create bar button
let barButton = UIBarButtonItem(customView: button)
navigationItem.leftBarButtonItem = barButton
After that create your function for button tap:
#objc fileprivate func callMethod() {
print("Your code here")
}

Swift 5: My barbutton in navigationbar wasn't fit

Here is my image:
And I put it in rightbarbuttonitem, and it is not fit with area.
This is result
Here is my code:
let button = UIButton(type: .custom)
button.setImage(UIImage (named: "Group 4"), for: .normal)
button.frame = CGRect(x: 0.0, y: 0.0, width: 35, height: 35)
//button.addTarget(target, action: nil, for: .touchUpInside)
let barButtonItem = UIBarButtonItem(customView: button)
self.navigationItem.rightBarButtonItem = barButtonItem
Please help me!!!
Can you try this instead of your code?
let rightBarButtonItem = UIButton(frame: CGRect(x: 0, y: 0, width: 36, height: 36))
rightBarButtonItem.setBackgroundImage(UIImage(named: "Group 4"), for: .normal)
rightBarButtonItem.addTarget(self, action: #selector(actionRightBar), for: .touchUpInside)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightBarButtonItem)

NavigationBarButton Item Not able to align vertically?

Hi I have tried to align the navigationbar button item position center in the navigation bar. I have changed the navigation bar height programmatically so I have able to align title its working fine, but same I have tried leftbarbutton and rightbarbutton. its has not vertically aligned center. I have tried these programmatically
Title Alignment
self.navigationController!.navigationBar.setTitleVerticalPositionAdjustment(-15, forBarMetrics: .Default)
RightBarButton:
let button: UIButton = UIButton(type: .Custom)
button.setImage(buttonImage, forState: UIControlState.Normal)
button.addTarget(self, action: #selector(self.toggleRight), forControlEvents: UIControlEvents.TouchUpInside)
button.frame = CGRectMake(0, 0, 40, 40)
let rightButton = UIBarButtonItem(customView: button)
// rightButton.imageInsets = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0)
let negativeSpacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
negativeSpacer.width = -10;
self.navigationItem.rightBarButtonItems = [negativeSpacer ,rightButton ]
LeftBarButton:
let button: UIButton = UIButton(type: .Custom)
button.setImage(buttonImage, forState: UIControlState.Normal)
button.addTarget(self, action: #selector(self.toggleLeft), forControlEvents: UIControlEvents.TouchUpInside)
button.frame = CGRectMake(0, 0, 40, 40)
let leftButton = UIBarButtonItem(customView: button)
let negativeSpacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
negativeSpacer.width = -10;
self.navigationItem.leftBarButtonItems = [negativeSpacer,leftButton ]
Please find the screenshot.

Custom Bar Button with Image And Title

I got the problem when i use Custom Bar button with image and title, I use below code
let button = UIButton(type: .system)
button.titleEdgeInsets.left = 5
button.setImage(buttonImg, for: .normal)
button.setTitle(title, for: .normal)
button.sizeToFit()
button.addTarget(self, action: #selector(self.popVC), for: .touchUpInside)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button)
And Here is the problem snippet. Text not show clearly
Try this:
let backButton = UIButton(type: .system)
backButton.frame = CGRect(x: 0, y: 0, width: 500, height: 30)
backButton.setImage(buttonImg, for: .normal)
backButton.setTitle(title, for: .normal)
backButton.addTarget(self, action: #selector(self.popVC(_:)), for: .touchUpInside)
backButton.sizeToFit()
let backBarButton = UIBarButtonItem(customView: backButton)
self.navigationItem.leftBarButtonItem = backBarButton

BarButtonItem not appearing

I have a simple + button on the right side to add rows to my tableView, however the button is not appearing. This is how i implement it
let AddButton = UIButton()
AddButton.setTitle("+", forState: .Normal)
let AddView = UIBarButtonItem(customView: AddButton)
self.navigationItem.rightBarButtonItem = AddView
AddButton.addTarget(self, action: #selector(self.addRow), forControlEvents: UIControlEvents.TouchUpInside)
Note, I use the same exact implementation for my hamburger button which does appear. This is the working hamburger button implementation
let navicon = UIButton(type: UIButtonType.System)
navicon.setImage(defaultMenuImage(), forState: UIControlState.Normal)
navicon.frame = CGRectMake(0, 0, 30, 30)
let menu = UIBarButtonItem(customView: navicon)
self.navigationItem.leftBarButtonItem = menu
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
navicon.addTarget(self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)), forControlEvents: .TouchUpInside)
why is this happening?
Try:
let AddButton = UIButton(type: .Custom)
AddButton.frame = CGRectMake(0, 0, 44, 44)