后退按钮图片 - 在 Swift 中它叫什么?

Back Button Image - what is it called in Swift?

我正在尝试使用 Swift 的内部后退按钮图像。 我之前问过这个问题并得到了技术上正确的答案,它继承了以前的视图,但是如果我插入这段代码,你可以控制当前视图上的后退按钮。

// Takeover Back Button
self.navigationItem.hidesBackButton = false
let newBackButton = UIBarButtonItem(title: "<", style: .Plain, target: self, action: "segueBack")
navigationItem.leftBarButtonItem = newBackButton

这给了我一个 <,"ABC" 会给我 ABC 等,但我如何触发 Swift 来放置它的内部后退按钮图像。下面的代码不起作用,但我认为是正确的。

let backImg: UIImage = UIImage(named: "BACK_BUTTON_DEFAULT_ICON")!
navigationItem.leftBarButtonItem!.setBackgroundImage(backImg, forState: .Normal, barMetrics: .Default)

有人知道怎么做吗?

尝试将自定义视图添加为后退按钮,如

var backButton = UIButton(frame: CGRectMake(0, 0, 70.0, 70.0))
var backImage = UIImage(named: "backBtn")
backButton.setImage(backImage, forState: UIControlState.Normal)
backButton.titleEdgeInsets = UIEdgeInsetsMake(10.0, 10.0, 10.0, 0.0)
backButton.setTitle("Back", forState: UIControlState.Normal)
backButton.addTarget(self, action: "buttonPressed", forControlEvents: UIControlEvents.TouchUpInside)
var backBarButton = UIBarButtonItem(customView: backButton)

var spacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
spacer.width = -15

self.navigationItem.leftBarButtonItems = [spacer,backBarButton]

它看起来与 iOS 后退按钮相同

如果您想获得默认的后退按钮图像,箭头是 class 类型 _UINavigationBarBackIndicatorView。

接下来是破解,

        UIImage *imgViewBack ;

        for (UIView *view in self.navigationController.navigationBar.subviews) {
            // The arrow is a class of type _UINavigationBarBackIndicatorView. This is not any of the private methods, so I think
            // this is fine for the AppStore...
            if ([NSStringFromClass([view class]) isEqualToString:@"_UINavigationBarBackIndicatorView"]) {
                // Set the image from the Default BackBtn Imageview
                UIImageView *imgView = (UIImageView *) view;
                if(imgView){
                    imgViewBack = imgView.image ;
                }
            }
        }

我为这个问题纠结了一段时间。最后我用下面的代码得到了背面图像:

let backImage = navigationController?.navigationBar.subviews[2].subviews[0].subviews[0].subviews[0] as! UIImageView).image

在 运行 上面的代码之前,确保显示后退按钮。然后你可以将 backImage 保存到任何你想要的地方。

这是我得到的backImage

这是我的解决方案:

override func viewDidLoad() {

...

let buttonBack = UIBarButtonItem(image: UIImage(named: "backButton"), style: .plain, target: self, action: #selector(buttonSavePressed(_:)))
self.navigationItem.leftBarButtonItem = buttonBack
let backButton = UIButton(frame: CGRect(x: 0, y: 0, width: 24.0, height: 24.0))
let backImage = UIImage(named: "backButton")
backButton.setImage(backImage, for: .normal)
backButton.setTitle("Back", for:.normal)
if #available(iOS 13.0, *) {
    backButton.setTitleColor(.link, for: .normal)
} else {
    backButton.setTitleColor(.blue, for: .normal)
}
backButton.addTarget(self, action:#selector(buttonSavePressed(_:)), for: .touchUpInside)
let backBarButton = UIBarButtonItem(customView: backButton)
let spacer = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
spacer.width = -15
self.navigationItem.leftBarButtonItems = [spacer,backBarButton]

}

@objc func buttonBackPressed(_ sender: Any) {

 ...
}

试试这个来替换后退按钮图像:

let back_image = UIImage(named: "btn_back")
self.navigationBar.backIndicatorImage = back_image
self.navigationBar.backIndicatorTransitionMaskImage = back_image

如果您不喜欢“后退”标题,您也可以添加:

self.navigationBar.topItem?.title = ""