加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

用键盘上的按钮显示UIView,比如Skype,Viber messengers(Swift,iO

发布时间:2020-12-14 02:26:02 所属栏目:Windows 来源:网络整理
导读:我想创建附件视图,放置在输入附件视图下,通过键盘,如Skype App或Viber: 我已经问过这样的问题here了,但是这个问题的建议解决方案并不那么优雅,因为当我将滚动视图拖到顶部时,我希望我的附件UIView向下移动键盘(我使用UIScrollViewKeyboardDismissModeIntera
我想创建附件视图,放置在输入附件视图下,通过键盘,如Skype App或Viber:

enter image description here

我已经问过这样的问题here了,但是这个问题的建议解决方案并不那么优雅,因为当我将滚动视图拖到顶部时,我希望我的附件UIView向下移动键盘(我使用UIScrollViewKeyboardDismissModeInteractive).

所以我创建了一个函数,找出视图,键盘和我的自定义输入附件视图放在哪里:

func findKeyboardView() -> UIView? {
       var result: UIView? = nil

       let windows = UIApplication.sharedApplication().windows
       for window in windows {
           if window.description.hasPrefix("<UITextEffectsWindow") {
               for subview in window.subviews {
                   if subview.description.hasPrefix("<UIInputSetContainerView") {
                       for sv in subview.subviews {
                           if sv.description.hasPrefix("<UIInputSetHostView") {
                               result = sv as? UIView
                               break
                           }
                       }
                       break
                   }
               }
              break
           }
       }
       return result
   }

enter image description here

之后,我添加了我的自定义UIView并创建了一些约束:

func createAttachView() {
        attach = MessageChatAttachmentsView(frame: CGRectZero)
        let newView = findKeyboardView()
        newView!.addSubview(attach!)
        newView!.addConstraint(NSLayoutConstraint(item: accessoryView,attribute: .Bottom,relatedBy: .Equal,toItem: attach!,attribute: .Top,multiplier: 1.0,constant: 0.0))
        attach!.addConstraint(NSLayoutConstraint(item: attach!,attribute: .Height,toItem: nil,attribute: .NotAnAttribute,constant: 260))
        attach!.addConstraint(NSLayoutConstraint(item: attach!,attribute: .Width,constant: 320))
    }

这会在输入附件视图和键盘上方创建自定义UIView,当我向上滚动时它会移动.但是当我想按下按钮时,我按下键盘上的一个键.

enter image description here

enter image description here

那么如何将此视图移动到UIInputSetHostView中的视图层次结构顶部?

解决方法

哦,感谢Brian Nickel,我发现也许不是优雅但非常简单的解决方案.所以我已经覆盖inputAccessoryView以在键盘上创建工具栏.所以基本上,如果我按下这个工具栏上的附加按钮,我想看到另一个inputView,而不是键盘.
所以在我的自定义输入附件视图类中,我创建了一些隐藏的textView:

class MessageChatInputAccessoryView : UIToolbar {
    var textView:UITextView!  //textView for entering text
    var sendButton: UIButton! //send message
    var attachButton: UIButton! // attach button "+"
    var attachTextView:UITextView! --> this one 

    override init(frame: CGRect) {
        super.init(frame: frame)
        .....
        ..... 
        attachTextView = UITextView(frame: CGRectZero)
        attachTextView.alpha = 0.0
        self.addSubview(attachTextView)
        ....
     }

所以在我的主视图控制器中,我创建了函数,为这个新创建的attachTextView重新初始化inputView,如下所示:

func attach(sender: UIButton) {
     if attachMenuIsShown {
          accessoryView.attachTextView.inputView = accessoryView.textView.inputView
          accessoryView.attachTextView.reloadInputViews()
          attachMenuIsShown = false
       } else {
           accessoryView.attachTextView.becomeFirstResponder()
           accessoryView.attachTextView.inputView = MessageChatAttachmentsView(frame: CGRectZero)
           accessoryView.attachTextView.reloadInputViews()
           attachMenuIsShown = true
    }

}

因此,当我按下附加按钮时,我的attachTextView成为第一响应者,而不是重新初始化此textView的输入视图.我在输入附件视图下方获得了附件视图.当我再次按下附加按钮时,我使用默认的inputView为我的主textView重新初始化inputView,这是键盘视图.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读