【问题标题】:How to move UIButton into a keypad when user start typing用户开始键入时如何将 UIButton 移动到键盘中
【发布时间】:2021-11-17 18:11:02
【问题描述】:

我在屏幕底部有一个 UIButton,当用户在 UITextView 中键入时,我希望该按钮像我附加的屏幕截图一样附加到键盘上。请提供一些示例代码。

设计

【问题讨论】:

    标签: ios swift uibutton keypad


    【解决方案1】:

    根据您的问题,您只需将按钮添加到文本字段 inputAccessoryView。

    yourTextField.inputAccessoryView = confirmButton
    

    要处理原始按钮位置,请临时创建一个与原始按钮相同的按钮,包括方法名称。

      let tempButton = UIButton(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 30))
        tempButton.backgroundColor = UIColor.blue
        tempButton.setTitle("YourConfirmButton", for: .normal)
        tempButton.setTitleColor(UIColor.white, for: .normal)
        tempButton.addTarget(self, action: #selector(self.onTapConfirmButton), for: .touchUpInside)
        yourTextField.inputAccessoryView = tempButton
    

    【讨论】:

    • 当我们关闭键盘时,如何使按钮与第一个设计中的位置相同?
    • @Adbul Hoque Nuri
    • @JOhnMic 检查更新的答案。
    • 所以你是说如果我自动关闭我的键盘按钮会进入同一个地方?
    • 您原来的按钮不会被取代。您只需要创建原始副本的克隆以显示在键盘上方。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多