我看到了this question用於將圖像添加到左側導航欄項目的答案。我成功地做到了。圖像和文本的導航欄按鈕項目。 swift
但我也想添加一個小圖片旁邊的文字。例如,我想顯示用戶有多少個硬幣。所以我需要顯示一個硬幣圖像和它旁邊的硬幣數量。如何做呢?
我試圖設置標題(與「123」),但我只看到圖像,而不是標題。這我的代碼:
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
謝謝。