将 UIButton 添加到工具栏 swift 5

Adding UIButton to toolbar swift 5

所以我已经阅读了其他堆栈溢出年龄并且 none 似乎对我有所帮助。我有一段代码可以创建我的 UIButton,它在这里:

let haveAccountButton: UIButton = {
    let HColor = UIColor(red: 89/255, green: 156/255, blue: 120/255, alpha: 1)
    let HFont = UIFont.systemFont(ofSize: 16)
    let HSColor = UIColor(red: 239/255, green: 47/255, blue: 102/255, alpha: 1)

    let HButton = UIButton(type: .system)
    let attributedTitle = NSMutableAttributedString(string:
        NSLocalizedString("Already have an account?", comment: ""), attributes: [NSAttributedString.Key.foregroundColor: UIColor.lightGray, NSAttributedString.Key.font: HFont])
    attributedTitle.append(NSAttributedString(string: NSLocalizedString(" Sign In", comment: ""), attributes: [NSAttributedString.Key.foregroundColor: HSColor, NSAttributedString.Key.font: HFont]))
    HButton.addTarget(self, action: #selector(signinAction), for: .touchUpInside)

    HButton.setAttributedTitle(attributedTitle, for: .normal)

    return HButton
}()

我已经在 viewDidLoad 中设置了 navigationController?.isToolbarHidden = false。我的问题是如何让按钮出现在工具栏中?

更新:此代码 运行 当前没有工具栏的方式如下:

import Foundation
import UIKit

class SignUpControllerSave: UIViewController {


let haveAccountButton: UIButton = {
    let HColor = UIColor(red: 89/255, green: 156/255, blue: 120/255, alpha: 1)
    let HFont = UIFont.systemFont(ofSize: 16)
    let HSColor = UIColor(red: 239/255, green: 47/255, blue: 102/255, alpha: 1)

    let HButton = UIButton(type: .system)
    let attributedTitle = NSMutableAttributedString(string:
        NSLocalizedString("Already have an account?", comment: ""), attributes: [NSAttributedString.Key.foregroundColor: UIColor.lightGray, NSAttributedString.Key.font: HFont])
    attributedTitle.append(NSAttributedString(string: NSLocalizedString(" Sign In", comment: ""), attributes: [NSAttributedString.Key.foregroundColor: HSColor, NSAttributedString.Key.font: HFont]))
    HButton.addTarget(self, action: #selector(signinAction), for: .touchUpInside)

    HButton.setAttributedTitle(attributedTitle, for: .normal)

    return HButton
}()

//potential gradient background if wanted
func setGradientBackground() {
    let top_Color = UIColor(red: 203/255, green: 215/255, blue: 242/255, alpha: 1.0).cgColor
    let bottom_Color = UIColor(red: 181/255, green: 199/255, blue: 242/255, alpha: 1.0).cgColor
    let gradientLayer = CAGradientLayer()
    gradientLayer.colors = [top_Color, bottom_Color]
    gradientLayer.locations = [0, 1]
    gradientLayer.frame = self.view.bounds
    self.view.layer.insertSublayer(gradientLayer, at: 0)
}

override func viewDidLoad() {
    super.viewDidLoad()
    //view.backgroundColor = .yellow
    navigationController?.isNavigationBarHidden = true
    navigationController?.isToolbarHidden = false
    navigationItem.title = NSLocalizedString("Membership", comment: "")
    setupHaveAccountButton()
}

override func viewWillAppear(_ animated: Bool) {
    navigationController?.isNavigationBarHidden = true
    navigationController?.isToolbarHidden = false
    setGradientBackground()
    super.viewWillAppear(animated)
}

@objc func signinAction() {
    navigationController?.popViewController(animated: true)
}

fileprivate func setupHaveAccountButton() {
    view.addSubview(haveAccountButton)
    haveAccountButton.anchors(top: nil, topPad: 0, bottom: view.safeAreaLayoutGuide.bottomAnchor,
                              bottomPad: 8, left: view.leftAnchor, leftPad: 0, right: view.rightAnchor,
                              rightPad: 0, height: 20, width: 0)
}

}

更新:之前的答案确实允许按钮现在位于工具栏上,但按钮目标操作不起作用。我尝试在可能的地方添加 .sizeToFit() 并查看了很多网站都无济于事。有人知道我该如何解决按钮目标问题吗?

此代码应按您希望的方式运行:

class SignUpControllerSave: UIViewController {

    /* the other code you posted can be used without changes */

    fileprivate func setupHaveAccountButton() {
        toolbarItems = [
            UIBarButtonItem(customView: haveAccountButton)
        ]
    }

}

因为这似乎会导致点击处理程序出现问题,请试试这个(看起来几乎一样 - 除了字体大小):

class SignUpControllerSave: UIViewController {


    fileprivate func setupHaveAccountButton() {
        let haveAccountLabel = UILabel()
        haveAccountLabel.font = UIFont.systemFont(ofSize: UIFont.buttonFontSize)
        haveAccountLabel.text = NSLocalizedString("Already have an account?", comment: "")
        haveAccountLabel.textColor = .lightGray

        toolbarItems = [
            UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil),
            UIBarButtonItem(customView: haveAccountLabel),
            UIBarButtonItem(title: "Sign In", style: .plain, target: self, action: #selector(self.signinAction)),
            UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        ]
        navigationController?.toolbar.tintColor = UIColor(red: 239/255, green: 47/255, blue: 102/255, alpha: 1)
    }

}