1

I saw the answer for this question for adding an image to the left nav bar item. And I did it successfully.

But I also want to add a small text next to the image. For example I want to show how many coins the user have. So I need to show a coins image and the number of coins next to it. How can it be done?

I tried to set title (with "123"), but I only see the image and not the title. This my code:

let button = UIButton.init(type: .custom)
    button.setImage(UIImage.init(named: "coins.png"), for: UIControlState.normal)
    button.addTarget(self, action:#selector(self.callMethod), for: UIControlEvents.touchUpInside)
    button.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30)
    button.setTitle("123", for: UIControlState.normal)
    let barButton = UIBarButtonItem.init(customView: button)
    self.navigationItem.leftBarButtonItem = barButton

Thanks.

4 Answers 4

6

Since the bar button item is initialized with a custom view, you can add a UIImage and a UILabel to one view, along with a UIButton to capture the touch event, and pass that into the initializer.

    // the UIButton is simply there to capture touch up event on the entire bar button view.
    let button = UIButton.init(type: .custom)
    button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
    let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
    imageView.image = UIImage(named: "coins")
    let label = UILabel(frame: CGRect(x: 35, y: 0, width: 50, height: 30))
    label.text = "1234"
    let buttonView = UIView(frame: CGRect(x: 0, y: 0, width: 90, height: 30))
    button.frame = buttonView.frame
    buttonView.addSubview(button)
    buttonView.addSubview(imageView)
    buttonView.addSubview(label)
    let barButton = UIBarButtonItem.init(customView: buttonView)
    self.navigationItem.leftBarButtonItem = barButton

You might have to adjust the frames accordingly for your own uses, of course.

Sign up to request clarification or add additional context in comments.

Comments

0

I think your problem is the width it's 30 try to make it bigger like 50

let buttonContainer:UIView = UIView()
buttonContainer.frame = CGRect(x: 0, y: 0, width: 50, height: 32)

let image = UIImage(named: "coins")

let button = UIButton.init(type: .system)
button.setImage(image, for: .normal)
button.frame = buttonContainer.frame
button.setTitle("sss", for: .normal)      
button.addTarget(self, action: #selector(action(_:)), for: .touchUpInside)

buttonContainer.addSubview(button)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: buttonContainer)

Comments

0

You can use backgroundImage for image and setTitle for test to the button. follow this link definitely you will get idea:

image for nav bar button item swift

Comments

-2

Use self.navigationItem.leftBarButtonItems that is an array of UIBarButtonItem

let button = UIBarButtonItem()
let text = UIBarButtonItem()

self.navigationItem.leftBarButtonItems = [button, text]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.