ios - subview 加载但没有一个按钮可以工作 ios swift

标签 ios swift uiview uiviewcontroller custom-keyboard

我有一个自定义键盘项目,其中包含一个包含collectionView和scrollView的 Storyboard。我还有一个用于搜索栏的文本字段,它会拉出一个包含键盘布局的 Nib ,我可以使用它在键盘内进行搜索。当包含键盘的 subview 变为事件状态时,我无法按任何按钮。我尝试在激活时将 subview 置于前面,并尝试在隐藏 collectionView 时将主视图发送到后面。我可以做什么来使用 subview 中的按钮?

Before viewDidLoad

    let filename = "buttonClick"
    let ext = "m4a"
    var soundId: SystemSoundID = 0

    @objc func typeKey(sender : UIButton)
    {
        AudioServicesPlaySystemSound(soundId)
        self.key.txtSearch.text = "\(self.key.txtSearch.text!)\((sender.titleLabel?.text!)!)"
        if(self.key.txtSearch.text != "")
        {
            isCaps = false
            updateKeyBoard()
        }
    }


    @objc func typeSpaceKey(sender : UIButton)
    {
        AudioServicesPlaySystemSound(soundId)
        self.key.txtSearch.text = "\(self.key.txtSearch.text!) "
    }
    @objc func typeDoneKey(sender : UIButton)
    {
        AudioServicesPlaySystemSound(soundId)
        hideTextField()
    }
    @objc func typeNumKey(sender : UIButton)
    {
        AudioServicesPlaySystemSound(soundId)
        isNum = !isNum
        updateKeyBoard()
        // self.key.txtSearch.text = "\(self.key.txtSearch.text!)\((sender.titleLabel?.text!)!)"
    }
    @objc func typeBackSpaceKey(sender : UIButton)
    {
        AudioServicesPlaySystemSound(soundId)
        self.key.txtSearch.text = "\(self.key.txtSearch.text!.dropLast())"
        if(self.key.txtSearch.text == "")
        {
            isCaps = true
            updateKeyBoard()
        }
    }

    @objc func typeCapsKey(sender : UIButton)
    {
        AudioServicesPlaySystemSound(soundId)
        if(isNum)
        {
            isFirst = !isFirst
        }
        else
        {
            isCaps = !isCaps
        }
        updateKeyBoard()
    }

    @objc func updateKeyBoard()
    {

        if(isCaps)
        {
            self.key.btnCap.setImage(#imageLiteral(resourceName: "cap_F"), for: .normal)
        }
        else
        {
            self.key.btnCap.setImage(#imageLiteral(resourceName: "caps"), for: .normal)
        }
        var count = 0
        for btn in buttons
        {
            if(!isNum)
            {
                if(isCaps)
                {
                    btn.setTitle("\(arrCapOn[count])", for: .normal)
                }
                else
                {
                    btn.setTitle("\(arrCapOff[count])", for: .normal)
                }
            }
            else
            {
                if(isFirst)
                {
                    btn.setTitle("\(arrNumCapOn[count])", for: .normal)
                }
                else
                {
                    btn.setTitle("\(arrNumCapOff[count])", for: .normal)
                }
            }
            count = count + 1
        }
    }

    @objc func activeTextField()
    {

        self.key.btnBack.isHidden = false
        self.key.txtSearch.becomeFirstResponder()
        self.key.constLeftAchor.constant = 40
        self.key.constSideKeyboard.constant = 8
        self.key.constLeftAchorView.constant = 32
        UIView.animate(withDuration: 0.2) {
            self.key.viewKeyboard.transform = CGAffineTransform(translationX: 0, y: 0)
            self.key.btnBack.alpha = 1.0
            self.key.layoutIfNeeded()
        }

        self.key.btnTextFieldSelect.isHidden = true



    }
    @objc func hideTextField()
    {
        self.key.btnBack.isHidden = true
        self.key.txtSearch.resignFirstResponder()
        self.key.btnTextFieldSelect.isHidden = false
        self.key.constSideKeyboard.constant = 400
        self.key.constLeftAchor.constant = 24
        self.key.constLeftAchorView.constant = 16
        UIView.animate(withDuration: 0.2) {
            self.key.viewKeyboard.transform = CGAffineTransform(translationX: self.view.frame.width, y: 0)
            self.key.btnBack.alpha = 0.0
            self.key.layoutIfNeeded()

        }
    }

    @objc func hideArticles(){
        self.categoriesScrollView.isHidden = true
        self.collectionview.isHidden = true
        //self.collectionview.isUserInteractionEnabled = false
        //self.view.bringSubview(toFront: key)
        //self.key.isUserInteractionEnabled = true

    }

    @objc func showArticles(){
        self.categoriesScrollView.isHidden = false
        self.collectionview.isHidden = false

    }

    @objc func handleTap(_ sender: UITapGestureRecognizer) {
        self.key.txtSearch.text = "Hello"
        self.inputView?.resignFirstResponder()

        print("Hello World")

    }


    func textFieldDidBeginEditing(_ textField: UITextField) {


    }

    override func textWillChange(_ textInput: UITextInput?) {
        // The app is about to change the document's contents. Perform any preparation here.
    }

    override func textDidChange(_ textInput: UITextInput?) {
        // The app has just changed the document's contents, the document context has been updated.

        var textColor: UIColor
        let proxy = self.textDocumentProxy
        if proxy.keyboardAppearance == UIKeyboardAppearance.dark {
            textColor = UIColor.white
        } else {
            textColor = UIColor.black
        }
        //   self.nextKeyboardButton.setTitleColor(textColor, for: [])

    }


    after viewDidLoad     
    if let soundUrl = Bundle.main.url(forResource: filename, withExtension: ext) {
                        AudioServicesCreateSystemSoundID(soundUrl as CFURL, &soundId)
                    }
                    self.key = Bundle.main.loadNibNamed("keyboard", owner: nil, options: nil)![0] as! keyboard
                    self.key.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 160)
                    //self.key.txtSearch.isUserInteractionEnabled = false
                    self.key.constLeftAchor.constant = 24

                    //  self.key.viewKeyboard.transform = CGAffineTransform(translationX: self.view.frame.width, y: 0)
                    self.key.btnBack.isHidden = true
                    self.key.btnBack.alpha = 0.0
                    self.key.btnTextFieldSelect.addTarget(self, action: #selector(activeTextField), for: .touchUpInside)
                    self.key.btnTextFieldSelect.addTarget(self, action: #selector(hideArticles), for: .touchUpInside)
                    self.key.btnBack.addTarget(self, action: #selector(hideTextField), for: .touchUpInside)
                    self.key.btnBack.addTarget(self, action: #selector(showArticles), for: .touchUpInside)

                    buttons.append(self.key.btnQ)
                    buttons.append(self.key.btnW)
                    buttons.append(self.key.btnE)
                    buttons.append(self.key.btnR)
                    buttons.append(self.key.btnT)
                    buttons.append(self.key.btnY)
                    buttons.append(self.key.btnU)
                    buttons.append(self.key.btnI)
                    buttons.append(self.key.btnO)
                    buttons.append(self.key.btnP)
                    buttons.append(self.key.btnA)
                    buttons.append(self.key.btnS)
                    buttons.append(self.key.btnD)
                    buttons.append(self.key.btnF)
                    buttons.append(self.key.btnG)
                    buttons.append(self.key.btnH)
                    buttons.append(self.key.btnJ)
                    buttons.append(self.key.btnK)
                    buttons.append(self.key.btnL)
                    buttons.append(self.key.btnZ)
                    buttons.append(self.key.btnX)
                    buttons.append(self.key.btnC)
                    buttons.append(self.key.btnV)
                    buttons.append(self.key.btnB)
                    buttons.append(self.key.btnN)
                    buttons.append(self.key.btnM)


                    for btn in buttons
                    {
                        btn.addTarget(self, action: #selector(typeKey), for: .touchUpInside)
                    }

                    self.key.btnCap.addTarget(self, action: #selector(typeCapsKey), for: .touchUpInside)
                    self.key.btnBackSpace.addTarget(self, action: #selector(typeBackSpaceKey), for: .touchUpInside)
                    self.key.btnSpace.addTarget(self, action: #selector(typeSpaceKey), for: .touchUpInside)
                    self.key.btnDone.addTarget(self, action: #selector(typeDoneKey), for: .touchUpInside)
                    //self.key.btnDone.addTarget(self, action: #selector(fetchSearch), for: .touchUpInside)
                    self.key.btnNum.addTarget(self, action: #selector(typeNumKey), for: .touchUpInside)

    view.addSubview(key)
            activeTextField()
            hideTextField()

最佳答案

请确保添加的 subview 已启用用户交互,并且不会发送到 super View 中。

关于ios - subview 加载但没有一个按钮可以工作 ios swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48075622/

相关文章:

ios - 从 swift 三元运算符返回引用

ios - 在 iOS 上,如何调用 applicationDidBecomeActive、loadView 和 viewDidLoad?

iphone - UIGraphicsContext 内存泄漏

ios - 从一个类调用方法并在另一个类中使用

ios - 游戏套件和 iPhone

ios - UILabel 确实得到了圆角

ios - 如何为 UIView 的任何特定边缘添加虚线边框?

ios - NSPredicate 与 NSDate 按天/月过滤但不是年

ios - 在 UIImageView 上设置顶部约束会导致 UILabel 的高度固定