swift - 导航控制器删除后退按钮箭头

时间:2015-05-05 16:57:57

标签: ios xcode swift uinavigationcontroller uiimage

我在swift中有一个应用程序,其中视图控制器嵌入在导航控制器中,现在在第二和第三个视图控制器中,在后退按钮上,有一个向后指向的蓝色箭头(默认情况下是那里)。我试图删除它,但当我想在那里有一个图像时,图像会全部被压扁。有谁知道如何删除后退按钮箭头,并用图像替换它? 非常感谢!

1 个答案:

答案 0 :(得分:5)

使用此....

设置文字:

let backBtn = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "BtnTapBack:")
navigationItem.leftBarButtonItem = backBtn
navigationItem.leftBarButtonItem?.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "YourFontName", size: 20)!], forState: UIControlState.Normal)

设置图片

let image = UIImage(named:"YourImageName") as UIImage!
var btnBack:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
btnBack.addTarget(self, action: "BtnTapBack:", forControlEvents: UIControlEvents.TouchUpInside)
btnBack.setImage(image, forState: UIControlState.Normal)
btnBack.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
btnBack.sizeToFit()
var myCustomBackButtonItem:UIBarButtonItem = UIBarButtonItem(customView: btnBack)
self.navigationItem.leftBarButtonItem  = myCustomBackButtonItem

操作转到上一个viewcontroller

@IBAction func BtnTapBack(sender: UIButton) {
        navigationController?.popViewControllerAnimated(true)
}