在 Swift 中关闭键盘时,UIToolbar 不再显示在视图中

Posted

技术标签:

【中文标题】在 Swift 中关闭键盘时,UIToolbar 不再显示在视图中【英文标题】:UIToolbar doesn't show again in view when dismissing keyboard in Swift 【发布时间】:2020-03-07 14:01:55 【问题描述】:

我有一个包含 UITextField 的 UIToolbar。我想在键盘上方显示工具栏,然后在文本字段内完成编辑后再次显示它。

问题是它在选择文本字段时工作。但是,隐藏键盘时工具栏从视图中消失。 应该怎么做才能将它重新显示到我的视图中?

我的 Swift 代码是: 添加 UITextFieldDelegate

class ATCChatThreadViewController: MessagesViewController, MessagesDataSource, MessageInputBarDelegate, UITextFieldDelegate 

创建工具栏如下

 func createToolbar()

        //Fixed space
        let fixed = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.fixedSpace, target: self, action: nil)
        fixed.width = 10

        //Camera
        //let img = UIImage(named: "camera-filled-icon")!.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        let img = UIImage.localImage("camera-filled-icon", template: true)
        let iconSize = CGRect(origin: CGPoint.zero, size: CGSize(width: 30, height: 30))
        let iconButton = UIButton(frame: iconSize)
        iconButton.setTitleColor(uiConfig.primaryColor, for: .normal)
        iconButton.setBackgroundImage(img, for: .normal)
        let cameraItem = UIBarButtonItem(customView: iconButton)
        cameraItem.tintColor = uiConfig.primaryColor
        iconButton.addTarget(self, action: #selector(cameraButtonPressed), for: .touchUpInside)

        //TextField true
        textFieldChat = UITextField(frame: CGRectMake(0,0,(self.view.frame.size.width - 100) ,30))
        textFieldChat.tintColor = uiConfig.primaryColor
        textFieldChat.textColor = uiConfig.inputTextViewTextColor
        textFieldChat.backgroundColor = uiConfig.inputTextViewBgColor
        textFieldChat.layer.cornerRadius = 14.0
        textFieldChat.layer.borderWidth = 0.0
        textFieldChat.font = UIFont.systemFont(ofSize: 16.0)
        textFieldChat.delegate = self
        textFieldChat.attributedPlaceholder = NSAttributedString(string: "Start typing...".localized(), attributes: [NSAttributedString.Key.foregroundColor: uiConfig.inputPlaceholderTextColor])

        let paddingView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 20))
        textFieldChat.leftView = paddingView
        textFieldChat.leftViewMode = .always
        let textFieldButton = UIBarButtonItem(customView: textFieldChat)

        //Fixed space
        let fixedTwo = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.fixedSpace, target: self, action: nil)
        fixedTwo.width = 10

        //Send Button
        let imgSend = UIImage.localImage("share-icon", template: true)
        let iconSendSize = CGRect(origin: CGPoint.zero, size: CGSize(width: 30, height: 30))
        let sendButton = UIButton(frame: iconSendSize)
        sendButton.setTitleColor(uiConfig.primaryColor, for: .normal)
        sendButton.setBackgroundImage(imgSend, for: .normal)
        let sendItem = UIBarButtonItem(customView: sendButton)
        sendItem
            .tintColor = uiConfig.primaryColor
        sendButton.addTarget(self, action: #selector(sendBtnPressedWith), for: .touchUpInside)

        //Flexible Space
        //        let flexible = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: self, action: nil)
        let flexible = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.fixedSpace, target: self, action: nil)
        flexible.width = 10

        //Toolbar
        var bottomsafeAreaHeight: CGFloat?
        if #available(ios 11.0, *) 
            bottomsafeAreaHeight = UIApplication.shared.windows.first$0.isKeyWindow ?.safeAreaInsets.bottom ?? 0
         else 
            // Fallback on earlier versions
            bottomsafeAreaHeight = bottomLayoutGuide.length
            bottomsafeAreaHeight = UIApplication.shared.keyWindow?.rootViewController?.bottomLayoutGuide.length ?? 0
        

        toolbar = UIToolbar(frame: CGRectMake(0,(self.view.frame.size.height - 116 - (bottomsafeAreaHeight ?? 0)),view.frame.width,50))
        toolbar.sizeToFit()
        toolbar.barTintColor = UIColor.f5f5f5ColorBg()
        toolbar.isTranslucent = false
        toolbar.tintColor = uiConfig.primaryColor
        toolbar.items = [fixed, cameraItem, fixed, textFieldButton,fixedTwo, sendItem,flexible]
        view.addSubview(toolbar)
    



   // MARK: - UITextFieldDelegate

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool 
    textFieldChat.inputAccessoryView = toolbar
    return true

func textFieldShouldEndEditing(_ textField: UITextField) -> Bool 
     view.addSubview(toolbar)
     self.toolbar.isHidden = false
     return true

【问题讨论】:

【参考方案1】:

如果只有聊天 etxfield 使用工具栏,则替换行:

view.addSubview(toolbar)

textFieldChat.inputAccessoryView = toolbar

删除textFieldShouldBeginEditingtextFieldShouldEndEditing这两个方法

【讨论】:

嗨@Van,如果我删除行 view.addSubview(toolbar),则不会显示任何内容,因为 3 个项目(相机、文本字段和发送)在 uitoolbar 内。我正在使用 uitabbar,我需要它们默认出现在标签栏上方,并在文本字段开始编辑时随键盘移动。

以上是关于在 Swift 中关闭键盘时,UIToolbar 不再显示在视图中的主要内容,如果未能解决你的问题,请参考以下文章

在自定义中从 TextField 中关闭键盘

不能在 Swift 中关闭导航控制器

从 uitableviewcell 内的文本字段中关闭 iPad 上的键盘

在 SWIFT 中关闭 segue 之前保存变量

Swift:在 AFNetworking 调用中关闭的未包装选项

firebase 在 Swift 中关闭重载表?