Swift 5: My barbutton in navigationbar wasn't fit - swift5

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)

Related

How to create overlapped bar button item programatically

I know how to create overlap shopping cart and quantity label with xib using uiimage like this
Now i'm trying to create overlap bar button items programatically but cannot figure out how to position the elements. My attempt:
My current code for bar button items:
let button: UIButton = UIButton(frame: CGRect(x: 0.0,
y: 0.0,
width: 24.0,
height: 24.0))
button.setImage(UIImage(named: "my cart", in: nil, compatibleWith: nil), for: .normal)
button.addTarget(self, action: #selector(cartButtonDidTapped), for: .touchUpInside)
let shoppingCartButton: UIBarButtonItem = UIBarButtonItem(customView: button)
shoppingCartQuantityLabel.layer.cornerRadius = 10
shoppingCartQuantityLabel.layer.masksToBounds = true
shoppingCartQuantityLabel.textColor = .white
shoppingCartQuantityLabel.backgroundColor = .red
shoppingCartQuantityLabel.textAlignment = .center
let shoppingCartQuantityLabelItem: UIBarButtonItem = UIBarButtonItem(customView: shoppingCartQuantityLabel)
navigationItem.rightBarButtonItems = [shoppingCartQuantityLabelItem, shoppingCartButton]
Idea here is to add the label as subview inside the button. You can adjust the label as per your needs from the below example,
let button: UIButton = UIButton(frame: CGRect(x: 0.0, y: 0.0, width: 24.0, height: 24.0))
button.setImage(#imageLiteral(resourceName: "my cart"), for: .normal)
let label = UILabel(frame: .init(origin: .init(x: 20, y: -8), size: .init(width: 20, height: 20)))
label.text = "12"
label.textAlignment = .center
label.textColor = .white
label.font = .systemFont(ofSize: 10)
label.backgroundColor = .red
label.layer.cornerRadius = 10
label.clipsToBounds = true
button.addSubview(label)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
You can use this code snippet also from Github: https://gist.github.com/freedom27/c709923b163e26405f62b799437243f4#gistcomment-2236010
then set badge to your last right bar button item as below
navigationItem.rightBarButtonItems = [shoppingCartButton]
let lastBarButton = navigationItem.rightBarButtonItems?.last
lastBarButton?.setBadge(text: "10", withOffsetFromTopRight: CGPoint(x: 38, y: -3), andColor: UIColor.red, andFilled: true, andFontSize: 12)

Swift4 addTarget at UIButton doesnt work in UIView

I programmatically created an UIButton and added it to a subview. AddTarget doesnt work there though. AddTarget only works if I add the button to the mainview.
self.view.addSubview(button)
instead of
ViewSystem.addSubview(button)
Doesnt anyone know why?
Here is the fullcode:
class ViewController: UIViewController {
var ViewSystem = UIView()
#objc func TestPressed(sender: UIButton?) {Test.text=String((sender?.tag)!)
func ButtonCreate () {
let button = UIButton()
button.frame = CGRect(x: 50, y: 100, width: 70, height: 70)
button.addTarget(self, action: #selector(TestPressed), for: .touchUpInside)
button.backgroundColor = UIColor.red
button.tag=5
ViewSystem.addSubview(button)
self.view.addSubview(ViewSystem)
}
}
This happening because you are set your button frame graterthen your view that's why your button not tapped.
you are not set your view frame and then how you can you set your button inside your view.
Here I update your ButtonCreate () function code it's working nicely.
func ButtonCreate () {
ViewSystem.frame = CGRect(x: 50, y: 100, width: 200, height: 70)
ViewSystem.backgroundColor = .blue
let button = UIButton()
button.frame = CGRect(x: 0, y: 0, width: 70, height: 70)
button.addTarget(self, action: #selector(TestPressed), for: .touchUpInside)
button.backgroundColor = UIColor.red
button.tag = 5
ViewSystem.clipsToBounds = false
ViewSystem.addSubview(button)
self.view.addSubview(ViewSystem)
}
I hope it's helpfull for you and save your time
You have to give frame to your ViewSystem. and ViewSystem's height width should be greater than button's X and Y.
var ViewSystem = UIView()
ViewSystem.frame = CGRect(x: 50, y: 100, width: 70, height: 70)
#objc func TestPressed(sender: UIButton?) {Test.text=String((sender?.tag)!)
func ButtonCreate () {
let button = UIButton()
button.frame = CGRect(x: 0, y: 0, width: 70, height: 70)
button.addTarget(self, action: #selector(TestPressed), for: .touchUpInside)
button.backgroundColor = UIColor.red
button.tag=5
ViewSystem.addSubview(button)
self.view.addSubview(ViewSystem)
}
}

Text color for uilabel not setting

I creating UIButton like that:
okBtn = UIButton()
okBtn.setTitle("OK", for: .normal)
okBtn.setTitle("OK", for: .selected)
okBtn.titleLabel?.textColor = .purpleLight
okBtn.backgroundColor = .red
okBtn.addTarget(self, action: #selector(didTapOKBtn), for: .touchUpInside)
self.addSubview(okBtn)
However, color is not setted, i added a screen to show how it looks.
Use title color
okBtn.setTitleColor(UIColor.blue, for: UIControlState.normal)
Use frame as required & add to your view as -
override func viewDidLoad() {
super.viewDidLoad()
let okBtn = UIButton(frame: CGRect(x: 20, y: 70, width: 50, height: 50))
okBtn.setTitle("OK", for: .normal)
okBtn.backgroundColor = .red
okBtn.setTitleColor(.white, for: .normal)
view.addSubview(okBtn)
}
Set the frame size first.
okBtn = UIButton(frame: CGRect(x: 10, y: 10, width: 100, height: 100)) //Sets the frame size on your viewController
okBtn.setTitle("OK", for: .normal)
okBtn.setTitleColor(UIColor.purple, for: .normal) //Sets the color of the text on the button
//There is no color as purpleLight. You need to set the rgb to get your desired color.
okBtn.backgroundColor = .red
okBtn.addTarget(self, action: #selector(didTapOKBtn), for: .touchUpInside)
self.view.addSubview(okBtn)

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.

Set constraint of bar button item 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: