1

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 :)

Community
  • 1
  • 1
Luis Felipe
  • 971
  • 1
  • 9
  • 18
  • Pay attention to custom View on uibarbuttonitem. Like this: self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: yourCustomView) it seems will help you. – stosha Jul 19 '15 at 09:11

1 Answers1

6

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

containView.addSubview(label)

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

self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: containView)
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Leo
  • 24,596
  • 11
  • 71
  • 92