返回按钮图像-它在Swift中被称为什么?

zte4gxcn  于 2023-10-15  发布在  Swift
关注(0)|答案(6)|浏览(97)

我正在尝试使用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)

有人知道怎么做吗?

eit6fx6z

eit6fx6z1#

尝试添加自定义视图作为返回按钮,如

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的返回按钮相同

kokeuurv

kokeuurv2#

我为这个问题挣扎了一段时间。最后,我得到了后面的图像,代码如下:

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

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

kmynzznz

kmynzznz3#

以下是我的解决方案:

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) {

 ...
}

zzoitvuj

zzoitvuj4#

如果要获取默认的后退按钮图像,则箭头是_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 ;
                }
            }
        }
4jb9z9bj

4jb9z9bj5#

尝试以下方法替换后退按钮图像:

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

如果你不喜欢“返回”标题,你也可以添加这个:

self.navigationBar.topItem?.title = ""
bnl4lu3b

bnl4lu3b6#

此解决方案使用自定义图标并为后退按钮的可缩放区域添加控件。

import SnapKit

private func setupBackButton() {
     
     let imageView = UIImageView()
     imageView.contentMode = .scaleAspectFit
     imageView.image = UIImage(named: "Back")
     
     let button = Button()
     button.addSubview(imageView)
     imageView.snp.makeConstraints { make in
         make.right.equalToSuperview()
         make.centerY.equalToSuperview()
         make.height.equalTo(20)
     }
     button.addTarget(self, action: #selector(onBack(_:)), for: .touchUpInside)

     // Somehow it's not tappable without this line
     button.backgroundColor = .black

     let barButtonItem = UIBarButtonItem(customView: button)
     navigationItem.leftBarButtonItem = barButtonItem
}

相关问题