3

我正在尝试创建一个自定义聊天工具栏来显示带有消息的文本视图,以及允许选择照片的按钮。今天早上我重新编写了我的代码,以非常密切地关注这个 WWDC 2017 关于文本输入的演讲。工具栏本身是一个 UIInputViewController,它使用自定义视图来定义它的 inputView。当我选择一个按钮时,我会呈现一个 UIImagePickerController,它工作正常。当我选择图像并返回对话屏幕时会出现问题......此时当我再次与工具栏交互时,它会在触摸时消失。这是从屏幕中删除 if 之前的堆栈跟踪:

#0  0x000000010d3b91fa in ConvoViewController.textViewDidEndEditing(UITextView) -> () at /Users/Joel/Documents/Software Projects/AtMe/AtMe/ConvoViewController.swift:480
#1  0x000000010d3b93ca in @objc ConvoViewController.textViewDidEndEditing(UITextView) -> () ()
#2  0x000000010f4b88b8 in -[UITextView resignFirstResponder] ()
#3  0x000000010ea9cc74 in -[UIView(Hierarchy) _removeFirstResponderFromSubtree] ()
#4  0x000000010ea9d2eb in __UIViewWillBeRemovedFromSuperview ()
#5  0x000000010ea9d09e in -[UIView(Hierarchy) removeFromSuperview] ()
#6  0x000000010f45cf5c in __55-[UIInputWindowController invalidateInputAccessoryView]_block_invoke ()
#7  0x000000010ec069a5 in -[UIResponder _preserveResponderOverridesWhilePerforming:] ()
#8  0x000000010f45ced4 in -[UIInputWindowController invalidateInputAccessoryView] ()
#9  0x000000010f45d889 in -[UIInputWindowController changeToInputViewSet:] ()
#10 0x000000010f456675 in -[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:] ()
#11 0x000000010f45e82f in -[UIInputWindowController setInputViewSet:] ()
#12 0x000000010f45614c in -[UIInputWindowController performOperations:withAnimationStyle:] ()
#13 0x000000010f0cca8a in -[UIPeripheralHost(UIKitInternal) setInputViews:animationStyle:] ()
#14 0x000000010ec0962d in -[UIResponder(UIResponderInputViewAdditions) reloadInputViews] ()
#15 0x000000010ec07ef1 in -[UIResponder _windowBecameKey] ()
#16 0x000000010ea6e9bb in -[UIWindow _makeKeyWindowIgnoringOldKeyWindow:] ()
#17 0x000000010ef33ab4 in -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) setFirstResponderIfNecessary] ()
#18 0x000000010ef37178 in -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) oneFingerTap:] ()
#19 0x000000010ef24f59 in -[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] ()
#20 0x000000010ef2cd57 in _UIGestureRecognizerSendTargetActions ()
#21 0x000000010ef2a70b in _UIGestureRecognizerSendActions ()
#22 0x000000010ef299ce in -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] ()
#23 0x000000010ef16152 in _UIGestureEnvironmentUpdate ()
#24 0x000000010ef15c43 in -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] ()
#25 0x000000010ef14e0a in -[UIGestureEnvironment _updateGesturesForEvent:window:] ()
#26 0x000000010ea60eea in -[UIWindow sendEvent:] ()
#27 0x000000010ea0da84 in -[UIApplication sendEvent:] ()
#28 0x000000010f1f15d4 in __dispatchPreprocessedEventFromEventQueue ()
#29 0x000000010f1e9532 in __handleEventQueue ()
#30 0x0000000110d14c01 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ ()
#31 0x0000000110cfa0cf in __CFRunLoopDoSources0 ()
#32 0x0000000110cf95ff in __CFRunLoopRun ()
#33 0x0000000110cf9016 in CFRunLoopRunSpecific ()
#34 0x00000001135fba24 in GSEventRunModal ()
#35 0x000000010e9f0134 in UIApplicationMain ()
#36 0x000000010d3ebed7 in main at /Users/Joel/Documents/Software Projects/AtMe/AtMe/AppDelegate.swift:15
#37 0x000000011258065d in start ()
#38 0x000000011258065d in start ()

我感觉 UIInputViewController 以某种方式无效,但我不知道如何测试它。有没有人有任何想法?我花了好几天的时间试图让这个工具栏没有错误,无论你如何处理它似乎都是不可能的。

对话视图控制器.swift

// MARK: - Input Accessory View (Controller)
fileprivate let chatToolbarView: ChatToolbarView = {
    let view = ChatToolbarView(frame: CGRect.zero, inputViewStyle: UIInputViewStyle.default)

    // Add selectors as targets to the toolbar buttons
    view.sendButton.addTarget(self, action: #selector(didPressSend(sender:)), for: UIControlEvents.touchUpInside)
    view.libraryButton.addTarget(self, action: #selector(didPressLibraryIcon(sender:)), for: UIControlEvents.touchUpInside)
    view.cameraButton.addTarget(self, action: #selector(didPressCameraIcon(sender:)), for: UIControlEvents.touchUpInside)

    return view
}()

// Wrapper view controller for the custom input accessory view
fileprivate let chatToolbarViewController = UIInputViewController()

override var inputAccessoryViewController: UIInputViewController? {
    // Ensure our input accessory view controller has it's input view set
    chatToolbarViewController.inputView = chatToolbarView

    // Return our custom input accessory view controller. You could also just return a UIView with
    // override func inputAccessoryView()
    return chatToolbarViewController
}


/** Action method which fires when the user taps the camera icon. */
@objc func didPressCameraIcon(sender: Any) {

    photoPickerDisplayed = true

    // Create picker, and set this controller as delegate
    let picker = UIImagePickerController()
    picker.delegate = self
    picker.allowsEditing = true
    picker.sourceType = UIImagePickerControllerSourceType.camera

    DispatchQueue.main.async {
        self.present(picker, animated: true, completion: nil)
    }
}

override var canBecomeFirstResponder: Bool { return true }
4

2 回答 2

1

我最终通过将自定义工具栏视图分配给视图控制器的inputAccessoryView解决了这个问题,而不是将其分配给视图控制器的 inputAccessoryViewController 的 inputView 属性。

最大的问题是,在原始代码中,任何以模态方式呈现的视图控制器(例如 UIImagePickerViewController)都会混淆 inputAccessoryViewController,并且会在返回到原始控制器时完全关闭。无论如何,使用控制器似乎被证明是不必要的,它更多地用于直接键盘定制而不是配件。

代码现在如下所示:

fileprivate let chatToolbarView: ChatToolbarView = {
    let view = ChatToolbarView(frame: CGRect.zero, inputViewStyle: UIInputViewStyle.default)

    // Add selectors as targets to the toolbar buttons
    view.sendButton.addTarget(self, action: #selector(didPressSend(sender:)), for: UIControlEvents.touchUpInside)
    view.libraryButton.addTarget(self, action: #selector(didPressLibraryIcon(sender:)), for: UIControlEvents.touchUpInside)
    view.cameraButton.addTarget(self, action: #selector(didPressCameraIcon(sender:)), for: UIControlEvents.touchUpInside)

    return view
}()


/** Provide the view controller's inputAccessoryView object. */
override var inputAccessoryView: UIView? { return chatToolbarView }


/** Give the view controller permission to become first responder. */
override var canBecomeFirstResponder: Bool { return true }
于 2017-08-01T16:28:08.267 回答
0

在你看来DidAppear

yourTextView.becomeFirstResponder()

这将触发您的自定义 inputView 再次打开。

希望这可以帮助!

于 2017-07-28T19:02:00.533 回答