Luis Felipe Luis Felipe - 1 month ago 21
iOS Question

How to make custom right UIBarButtonItem with image and label?

I actually found this same question here but for obj.c so with that I managed to do this:

trashNavButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
trashNavButton.addTarget(self, action: "trashItemClicked", forControlEvents: UIControlEvents.TouchUpInside)
trashNavButton = UIButton(frame: CGRectMake(0, 0, 50, 32))
trashNavButton.setTitle("\(trashCount)", forState: UIControlState.Normal)
trashNavButton.setTitleColor(UIColor(red: 229.0/255.0, green: 57.0/255.0, blue: 53.0/255.0, alpha: 1.0), forState: UIControlState.Normal)
trashNavButton.titleLabel?.textAlignment = NSTextAlignment.Right
trashNavButton.setImage(UIImage(named: "trashButton"), forState: UIControlState.Normal)
rightBarButton.customView = trashNavButton
self.navigationItem.rightBarButtonItem = rightBarButton

What I'm trying to do is to have a button on the right at the navigation bar, and I want that button to also have a label on it's left (you can see a great example on the question I mentioned above), and well this code works very well with the exception that the image is on the left and the label on the right and I want it to be the other way around, I'll appreciate your help :)

Leo Leo

Just as an example

let containView = UIView(frame: CGRectMake(0, 0,70, 40))

let label = UILabel(frame: CGRectMake(0, 0, 50, 40))
label.text = "Right"
label.textAlignment = NSTextAlignment.Center


let imageview = UIImageView(frame: CGRectMake(50, 10, 20, 20))
imageview.image = UIImage(named: "memoAccess")
imageview.contentMode = UIViewContentMode.ScaleAspectFill

self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: containView)