gpt4 book ai didi

ios - Swift:将 "Back"作为默认按钮文本设置为 "segue back"按钮

转载 作者:行者123 更新时间:2023-11-28 15:11:12 27 4
gpt4 key购买 nike

我正在寻找一种方法将所有后退按钮文本设置为“后退”,而不是默认显示 segue 来自的 View 的标题。

我有多个 View 和 segue,希望有比在所有 View Controller 中使用 PrepareForSegue 更简洁的方法。

这也会简化本地化,因为我让用户能够更改语言。

有人有任何建议或解决方案吗?

最佳答案

为 UIViewController 创建扩展(您可以为此创建不同的空文件,并确保导入 UIKIt)-:

import UIKit
extension UIViewController {


func leftBarButton(subView:UIView,labelView:UILabel,imageView:UIImageView) -> UIBarButtonItem{

let leftBarButton = UIBarButtonItem(customView: subView)
subView.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
subView.addSubview(imageView)
subView.addSubview(labelView)

imageView.leftAnchor.constraint(equalTo: subView.leftAnchor,constant: -8).isActive = true
imageView.topAnchor.constraint(equalTo: subView.topAnchor, constant: 2).isActive = true
imageView.heightAnchor.constraint(equalToConstant: 30).isActive = true
imageView.widthAnchor.constraint(equalToConstant: 30).isActive = true

labelView.leftAnchor.constraint(equalTo: imageView.rightAnchor,constant: 8).isActive = true
labelView.rightAnchor.constraint(equalTo: subView.rightAnchor).isActive = true
labelView.topAnchor.constraint(equalTo: subView.topAnchor).isActive = true
labelView.heightAnchor.constraint(equalToConstant: 40).isActive = true

return leftBarButton
}

func anotherMethod(){
_ = self.navigationController?.popViewController(animated: true)
}
}

在 Controller 类中-:

  var views : UIView = {
let subView = UIView()
//subView.backgroundColor = UIColor.green
return subView
}()

var imageView : UIImageView = {
var view = UIImageView()
view.translatesAutoresizingMaskIntoConstraints = false
view.image = UIImage(named: "Back")
return view
}()

var labelView: UILabel = {
var labeView = UILabel()
labeView.translatesAutoresizingMaskIntoConstraints = false
labeView.text = "Back"
labeView.textColor = UIColor.blue
return labeView
}()

// ViewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
//self.navigationBar.tintColor = UIColor.green
self.navigationItem.leftBarButtonItem = leftBarButton(subView: views, labelView: labelView, imageView: imageView)


}

关于ios - Swift:将 "Back"作为默认按钮文本设置为 "segue back"按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47608623/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com