如何使用图像和标签制作自定义的UIBarButtonItem?

我实际上在这里找到了同样的问题但是对于obj.c所以我设法做到了这一点:

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 

我要做的是在导航栏右侧有一个按钮,我希望该按钮左侧还有一个标签(你可以在我上面提到的问题上看到一个很好的例子),以及这个代码效果很好,除了图像在左边,标签在右边,我希望它是相反的方式,我将感激你的帮助:)

仅作为一个例子

 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)