将 UIButton 添加到工具栏 swift 5

Posted

技术标签:

【中文标题】将 UIButton 添加到工具栏 swift 5【英文标题】:Adding UIButton to toolbar swift 5 【发布时间】:2019-10-31 23:09:17 【问题描述】:

所以我已经阅读了其他堆栈溢出年龄,但似乎没有一个对我有帮助。我有一段代码在这里创建了我的 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() 并且查看了很多网站都无济于事。有人知道我该如何解决按钮目标问题吗?

【问题讨论】:

显示尝试使用此haveAccountButton 属性的代码。 不相关但变量名应以小写字母开头。这将使您的代码更易于阅读。 将来会修复,但我更新以显示我当前如何显示代码 我认为你不应该在工具栏上使用UIButton,而是使用UIBarButtonItem 有时我使用自定义的UIView 作为我的工具栏,如果原生UIToolBar 不能满足我的目的。 【参考方案1】:

这段代码应该按照你想要的方式工作:

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)
    


【讨论】:

由于某种原因,我现在无法单击该按钮。它不会把我带到我需要的页面。

以上是关于将 UIButton 添加到工具栏 swift 5的主要内容,如果未能解决你的问题,请参考以下文章

如何将 UIButton 添加到 Swift Playground?

Swift - 将目标添加到子视图内的 UIButton

Swift 3:将UIButton扩展添加到ViewController

如何在 Swift 3 中将 UIButton 添加到我的 UITableViewCell 中?

Swift 访问 UIButton 标签

Swift3 中心 UIbutton