在其上显示UIBarButton标题的图像

时间:2015-12-15 09:07:29

标签: ios swift uinavigationcontroller uibarbuttonitem

我试图制作一个显示图像和标题的自定义UIBarButton,到目前为止我已经这样做了:

enter image description here

我想要做的是在购物车图片上显示标题,而不是旁边的标题。

这是我写的代码:

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;

那么,我该怎么做?

1 个答案:

答案 0 :(得分: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;