微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

键盘扩展名布局问题

如何解决键盘扩展名布局问题

概述

我正在创建键盘扩展名。我希望它与ios系统一样,但有一些问题。在下面的说明中,我只会将我正在使用的代码放在前三行中,而仅用于字母(因此不需移位,退格,123,表情符号,空格,返回按钮)。

我在做什么

  1. 我为键盘创建了一个自定义UIView类
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,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,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),right: calculateSpace(row: midRow))
    case 2:
        return  UIEdgeInsets(top: CGFloat(topSpace),left: calculateSpace(row: bottomrow),right: calculateSpace(row: bottomrow))
    default:
        return UIEdgeInsets(top: 0,left: 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,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)
}

}

这是我设置班级的方式。

  1. 在我的InputViewController中,设置视图:
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
}

问题: 现在的问题是,当我在模拟器上运行键盘时,它在每种设备上看起来都不一样,我猜是因为宽度不同。我想知道是否有更好的方法来设置键盘和按键的高度和宽度。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。