概述:
我正在创建键盘扩展名。我希望它与ios系统一样,但有一些问题。在下面的说明中,我只会将我正在使用的代码放在前三行中,而仅用于字母(因此不需移位,退格,123,表情符号,空格,返回按钮)。
我在做什么:
class lettersKeyboard: UIView{
//View where I'll put all of the buttons
let mainView : UIView = {
let m = UIView(frame: UIScreen.main.bounds)
m.translatesAutoresizingMaskIntoConstraints = false
m.backgroundColor = .clear
return m
}()
//Collection view for buttons
var keyView: UICollectionView!
//I defined the values for space between buttons and on top.
let interButtonSpace = 8
let topSpace = 9
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit(){
//I set the keyboard height
if UIDevice.current.userInterfaceIdiom == .phone{
let heightConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutConstraint.Attribute.height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0.0, constant: 280)
addConstraint(heightConstraint)
} else if UIDevice.current.userInterfaceIdiom == .pad{
let heightConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutConstraint.Attribute.height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0.0, constant: 500)
addConstraint(heightConstraint)
}
addSubview(mainView)
setupKeyboard()
}
//Setup of collection view
func setupKeyboard(){
let layout = UICollectionViewFlowLayout()
if UIDevice.current.userInterfaceIdiom == .phone{
keyView = UICollectionView(frame: CGRect(x: 0.0, y: toolbar.frame.height , width: mainView.frame.width, height: 280), collectionViewLayout: layout)
} else if UIDevice.current.userInterfaceIdiom == .pad{
keyView = UICollectionView(frame: CGRect(x: 0.0, y: toolbar.frame.height , width: mainView.frame.width, height: 500), collectionViewLayout: layout)
}
keyView.setCollectionViewLayout(layout, animated: true)
keyView.translatesAutoresizingMaskIntoConstraints = false
keyView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "collectionCellId")
keyView.delegate = self
keyView.dataSource = self
mainView.addSubview(keyView)
}
//Number of sections
func numberOfSections(in collectionView: UICollectionView) -> Int {
3
}
//Items per section
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
switch section {
case 0:
return 10
case 1:
return 9
case 2:
return 7
default:
return 0
}
}
//Cells setup
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = keyView.dequeueReusableCell(withReuseIdentifier: "collectionCellId", for: indexPath)
cell.backgroundColor = .systemPink
return cell
}
//I wrote this back on SO so if there are any issues it's because of that. The calculateSpace function is below this one
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
let topRow = 10
let midRow = 9
let bottomRow = 7
switch section {
case 0:
return UIEdgeInsets(top: CGFloat(topSpace), left: calculateSpace(row: topRow), bottom: 0, right: calculateSpace(row: topRow))
case 1:
return UIEdgeInsets(top: CGFloat(topSpace), left: calculateSpace(row: midRow), bottom: 0, right: calculateSpace(row: midRow))
case 2:
return UIEdgeInsets(top: CGFloat(topSpace), left: calculateSpace(row: bottomRow), bottom: 0, right: calculateSpace(row: bottomRow))
default:
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
}
//Calculates the space occupied by the buttons and the space between them and returns the margin space the buttons have to have with mainView
func calculateSpace(row: Int) -> CGFloat{
let buttonsWidth = keyView.frame.width/13
let bSpace = CGFloat(row)*buttonsWidth
let iSpace = (row+2)*interButtonSpace
let cSpace = bSpace+CGFloat(iSpace)
let sideSpace = (frame.width-cSpace)/2
return sideSpace
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return CGFloat(interButtonSpace)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout:
UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let buttonsHeight = keyView.frame.height/5
let buttonsWidth = keyView.frame.width/13
return CGSize(width: buttonsWidth, height: buttonsHeight)
}
}
这是我设置班级的方式。
class KeyboardViewController: UIInputViewController {
var letters : lettersKeyboard = {
let m = lettersKeyboard(frame: CGRect(x: 0.0, y: 0.0, width: 414, height: 280))
m.translatesAutoresizingMaskIntoConstraints = false
//m.backgroundColor = .clear
return m
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(letters)
letters.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
letters.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
letters.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
letters.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
问题: 现在的问题是,当我在模拟器上运行键盘时,它在每种设备上看起来都不一样,我猜是因为宽度不同。我想知道是否有更好的方法来设置键盘和按键的高度和宽度。