我正在尝试使用Swift为iOS设备构建自定义键盘扩展。但是现在自定义键盘扩展按钮时遇到了一些麻烦。这是我的代码:
class KeyboardViewController: UIInputViewController {
var qButton: UIButton = {
var button = UIButton(type: .system)
button.translatesAutoresizingMaskIntoConstraints = false
button.backgroundColor = UIColor(white: 1.0, alpha: 1.0)
button.setTitleColor(UIColor.black, for: .normal)
button.layer.cornerRadius = 5
button.titleLabel?.font = UIFont.systemFont(ofSize: 22, weight: .regular)
button.setImage(UIImage(named: "q")?.withRenderingMode(.alwaysOriginal), for: .normal)
button.addTarget(self, action: #selector(keyPressed), for: .touchUpInside)
return button
}()
var firstStackRow: UIStackView = {
var firstStackRow = UIStackView()
firstStackRow.translatesAutoresizingMaskIntoConstraints = false
firstStackRow.axis = .horizontal
firstStackRow.distribution = .fillEqually
firstStackRow.spacing = 6
firstStackRow.backgroundColor = .lightGray
return firstStackRow
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(firstStackRow)
firstStackRow.translatesAutoresizingMaskIntoConstraints = false
firstStackRow.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 3).isActive = true
firstStackRow.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -3).isActive = true
firstStackRow.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 10.5).isActive = true
firstStackRow.heightAnchor.constraint(equalTo: self.view.heightAnchor, multiplier: 1/4, constant: -12).isActive = true
firstStackRow.addArrangedSubview(qButton)
qButton.heightAnchor.constraint(equalTo: firstStackRow.heightAnchor).isActive = true
qButton.topAnchor.constraint(equalTo: firstStackRow.topAnchor).isActive = true
}
}
但是似乎按钮“ q”无法显示图像。不知道那里出了什么问题。任何想法将不胜感激。