Skip to content

Instantly share code, notes, and snippets.

@timqzm
Last active July 5, 2017 12:53
Show Gist options
  • Save timqzm/df7f462f12d6efd9070a19c72a52f932 to your computer and use it in GitHub Desktop.
Save timqzm/df7f462f12d6efd9070a19c72a52f932 to your computer and use it in GitHub Desktop.
How to set PKHUD above keyboard AND inputAccessoryView? https://stackoverflow.com/questions/44840460/how-to-set-pkhud-above-inputaccessoryview
var activeTextField: UITextField?
@IBAction func logIn(_ sender: Any) {
// ...
HUD.flash(.labeledError(title: "blabla", subtitle: "blablalba"),
onView: activeTextField?.inputAccessoryView,
delay: 1.0)
// ...
}
func textFieldDidBeginEditing(_ textField: UITextField) {
activeTextField = textField // if keyboard is shown, show HUD.flash under inputAccessoryView
}
func textFieldDidEndEditing(_ textField: UITextField) {
activeTextField = nil // if keyboard isn't shown, just show simple HUD.flash
}
@PGLongo
Copy link

PGLongo commented Jul 1, 2017

var activeTextField: UITextField?

@IBAction func logIn(_ sender: Any) {
    // ...
    let customView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
    customView.backgroundColor = .red
    textfield?.inputAccessoryView = customView
    HUD.flash(.labeledError(title: "blabla", subtitle: "blablalba"), 
              onView: activeTextField?.inputAccessoryView, 
              delay: 1.0)
    // ...
}

func textFieldDidBeginEditing(_ textField: UITextField) {
    activeTextField = textField // if keyboard is shown, show HUD.flash under inputAccessoryView
}
    
func textFieldDidEndEditing(_ textField: UITextField) {
    activeTextField = nil // if keyboard isn't shown, just show simple HUD.flash
}

This should work. If you don't create an inputAccessoryView the default is nil!

@timqzm
Copy link
Author

timqzm commented Jul 3, 2017

@PGLongo sorry, I didn't provide stuff like creating inputAccessoryView or setting textField delegate. Your example works absolutely the same as my screenshot above, if you make height smaller (I've tried 44). Well, that's full code:

import PKHUD

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var textField: UITextField!
    var activeTextField: UITextField?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        textField.delegate = self
        
        let customView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 44))
        customView.backgroundColor = .red
        textField?.inputAccessoryView = customView
    }
    
    @IBAction func logIn(_ sender: Any) {
        HUD.flash(.labeledError(title: "blabla", subtitle: "blablalba"),
                  onView: activeTextField?.inputAccessoryView,
                  delay: 1.0)
    }
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        activeTextField = textField // if keyboard is shown, show HUD.flash under inputAccessoryView
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        activeTextField = nil // if keyboard isn't shown, just show simple HUD.flash
    }

} 

screen shot 2017-07-03 at 09 58 33

@timqzm
Copy link
Author

timqzm commented Jul 3, 2017

@PGLongo I've pushed the project, if you want to play with it https://github.com/timqzm/PKHUD-above-inputAccessoryView

@PGLongo
Copy link

PGLongo commented Jul 3, 2017

Maybe you want this:

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var textField: UITextField!
    var activeTextField: UITextField?
    let customView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
    
    override func viewDidLoad() {
        super.viewDidLoad()
        textField.delegate = self
        customView.backgroundColor = .red
    }
    
    @IBAction func logIn(_ sender: Any) {
        self.activeTextField?.inputView = self.customView
        self.activeTextField?.reloadInputViews()
        HUD.show(.progress, onView: self.activeTextField?.inputView)
    }
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        activeTextField = textField
        
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        activeTextField = nil // if keyboard isn't shown, just show simple HUD.flash
        
    }

}

In this way the HUD replace the keyboard

@timqzm
Copy link
Author

timqzm commented Jul 3, 2017

@PGLongo
NO, I don't want replace the keyboard. I just want to see it like this:
what_i_want
Is there a way to do so?

@PGLongo
Copy link

PGLongo commented Jul 4, 2017

You need to do this:
HUD.show(.progress, onView: UIApplication.shared.windows.last)

@timqzm
Copy link
Author

timqzm commented Jul 5, 2017

@PGLongo works perfectly, thx a lot!

screen shot 2017-07-05 at 15 24 33

@PGLongo
Copy link

PGLongo commented Jul 5, 2017

You are welcome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment