2015-12-15 92 views
1

我試圖做一個定製UIBarButton其顯示圖像和標題,到目前爲止,我已經做到了這一點:上顯示UIBarButton標題的圖像

enter image description here

我想要做的是顯示在購物車圖像上的標題是5,而不是在旁邊。

這是我寫的代碼:

let shopingCartBTN = UIButton(type: UIButtonType.Custom) 
//btnShowMenu.setImage(self.defaultMenuImage(), forState: UIControlState.Normal) 
shopingCartBTN.setImage(UIImage(named: "shopingCarBarIcon"), forState: UIControlState.Normal) 
shopingCartBTN.imageView?.image = UIImage(named: "shopingCarBarIcon") 
shopingCartBTN.frame = CGRectMake(0, 0, 30, 30) 
shopingCartBTN.setTitle("5", forState: .Normal) 
shopingCartBTN.addTarget(self, action: "", forControlEvents: UIControlEvents.TouchUpInside) 
let customBarItem = UIBarButtonItem(customView: shopingCartBTN) 
self.navigationItem.leftBarButtonItem = customBarItem; 

所以,我應該怎樣做呢?

回答

0

只要你需要增加Button

let shopingCartBTN = UIButton(type: UIButtonType.Custom) 
//btnShowMenu.setImage(self.defaultMenuImage(), forState: UIControlState.Normal) 
shopingCartBTN.setImage(UIImage(named: "shopingCarBarIcon"), forState: UIControlState.Normal) 
shopingCartBTN.imageView?.image = UIImage(named: "shopingCarBarIcon") 
shopingCartBTN.frame = CGRectMake(0, 0, 60, 30)//Just increase the width of button 
shopingCartBTN.setTitle("5", forState: .Normal) 
shopingCartBTN.addTarget(self, action: "", forControlEvents: UIControlEvents.TouchUpInside) 
let customBarItem = UIBarButtonItem(customView: shopingCartBTN) 
self.navigationItem.leftBarButtonItem = customBarItem; 
+0

你試試這個@donalddraper寬度是多少? –