Back Button Image - what is it called in Swift?

本秂侑毒 提交于 2019-12-28 16:14:12

问题


I am trying to use Swift's internal back button image. I have asked this question before and got the technically correct answer that it inherits it from the previous View, BUT if I insert this code you can control the back button on the current View.

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

That gives me a <, "ABC" would give me ABC etc but how do I trigger Swift to put up it's internal Back Button image. The code below doesn't work but I would have thought is along the right lines.

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

Has anyone worked out how to do this?


回答1:


Try to add custom view as back button like as

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]

It will look same as iOS back button




回答2:


I struggled with this question for a while. Finally I got the back image with the following code:

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

Before run the code above, make sure the back button is showing. Then you can save backImage to anywhere you want.

Here is the backImage I got.




回答3:


If you want to get the default back button image, the arrow is a class of type _UINavigationBarBackIndicatorView.

Here follows the hack,

        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 ;
                }
            }
        }


来源:https://stackoverflow.com/questions/31807997/back-button-image-what-is-it-called-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!