Slide UIInputView with UIViewController like Slack

旧巷老猫 提交于 2020-01-07 03:07:28

问题


I'd like to use the UIViewController's input accessory view like this:

override func canBecomeFirstResponder() -> Bool {
    return true
}

override var inputAccessoryView: UIView! {
    return self.bar
}

but the issue is that I have a drawer like view and when I slide the view open, the input view stays on the window. How can I keep the input view on the center view like Slack does it.

Where my input view stays at the bottom, taking up the full screen (the red is the input view in the image below):


回答1:


There are two ways to do this exactly like Slack doing it, Meiwin has a medium post here A Stickler for Details: Implementing Sticky Input Field in iOS to show how he managed to do this which he actually puts an empty UIView as an inputAccessoryView then track it’s coordinates on screen to know where to put his custom view in relation with the empty view, this way can be helpful if you are going to support SplitViewController on iPad, but if you are not interested in this way, you can see how I managed to do this like this image

Here is before swiping

Here is after

All I did was actually taking a snapshot from the inputAccessoryView window and putting it on the NavigationController of the TableViewController

I am using SideMenu from Jon Kent and it’s pretty easy to do it with the UISideMenuNavigationControllerDelegate

var isInputAccessoryViewEnabled = true {
    didSet {
        self.inputAccessoryView?.isHidden = !self.isInputAccessoryViewEnabled
        if self.isInputAccessoryViewEnabled {self.becomeFirstResponder()}
    }
}

func sideMenuWillAppear(menu: UISideMenuNavigationController, animated: Bool) {
    let inputWindow = UIApplication.shared.windows.filter({$0.className == "UITextEffectsWindow"}).first
    self.inputAccessoryViewSnapShot = inputWindow?.snapshotView(afterScreenUpdates: false)
    if let snapShotView = self.inputAccessoryViewSnapShot, let navView = self.navigationController?.view {
        navView.addSubview(snapShotView)
    }

    self.isInputAccessoryViewEnabled = false
}

func sideMenuDidDisappear(menu: UISideMenuNavigationController, animated: Bool) {
    self.inputAccessoryViewSnapShot?.removeFromSuperview()        
    self.isInputAccessoryViewEnabled = true
}

I hope that helps :)



来源:https://stackoverflow.com/questions/33326649/slide-uiinputview-with-uiviewcontroller-like-slack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!