Leaving inputAccessoryView visible after keyboard is dismissed

前端 未结 5 1831
-上瘾入骨i
-上瘾入骨i 2020-12-13 09:24

What I\'m trying to do is to create something similar to the \"find on page\" search function in Safari on iPad.

I\'m using a UIToolbar with some items

相关标签:
5条回答
  • 2020-12-13 10:10

    You may also need to work around the bug with the inputAccessoryView not respecting the safe area margins and thus not making room for home indicator thing on iPhone X: iPhone X how to handle View Controller inputAccessoryView?

    I found the easiest solution when you have a UIToolbar from a xib and you are also using that UIToolbar as the inputAccessoryView of a text field is to embed the toolbar in a UIView when you return it from your overridden inputAccessoryView, and make the containing UIView taller by the safeAreaInsets.bottom. (Other solutions suggest constraining the bottom of the toolbar to the safe area in a subclass, but this leads to constraint conflicts and also means the area under the toolbar is the wrong colour.) However, you have to also bear in mind that the text field can have focus even when there is no keyboard on the screen (for instance if there is an external keyboard), so you need to change the inputAccessoryView of the text view to this toolbar-within-a-UIView in that case as well. In fact it will probably make things simpler to just always use the containing view and adjust the size of it appropriately. Anyway, here's my override of inputAccessoryView:

        override var inputAccessoryView: UIView? {
    
        if toolbarContainerView == nil {
    
            let frame=CGRect(x: toolBar.frame.minX, y: toolBar.frame.minY, width: toolbar.frame.width, height: toolBar.frame.height+view.safeAreaInsets.bottom)
    
            toolbarContainerView = UIView(frame: frame)
    
        }
    
        if (toolbar.superview != toolbarContainerView) {
    
            //this is set to false when the toolbar is used above the keyboard without the container view
    
            //we need to set it to true again or else the toolbar will appear at the very top of the window instead of the bottom if the keyboard has previously been shown.
    
            toolbar.translatesAutoresizingMaskIntoConstraints=true
    
            toolbarContainerView?.addSubview(toolbar)
    
        }
    
        return toolbarContainerView
    
    }
    

    It would probably be a good idea to override viewSafeAreaInsetsDidChange to adjust the size of toolbarContainerView in that case, too.

    0 讨论(0)
  • 2020-12-13 10:11

    It's done like this:

    Assign your UIToolbar to a property in your view controller:

    @property (strong, nonatomic) UIToolbar *inputAccessoryToolbar;
    

    In your top view controller, add these methods:

    - (BOOL)canBecomeFirstResponder{
    
        return YES;
    
    }
    
    - (UIView *)inputAccessoryView{
    
        return self.inputAccessoryToolbar;
    
    }
    

    And then (optionally, as it usually shouldn't be necessary), whenever the keyboard gets hidden, just call:

    [self becomeFirstResponder];
    

    That way, your inputAccessoryToolbar will be both your view controller's and your text view's input accessory view.

    0 讨论(0)
  • 2020-12-13 10:12

    Update to Swift 4, based on prior answers. If you add toolbar via storyboards you can do this

    class ViewController: UIViewController {
    
        @IBOutlet weak var textField: UITextField!
        @IBOutlet var toolbar: UIToolbar!
    
        override var canBecomeFirstResponder: Bool {
            get {
                return true
            }
        }
    
        override var inputAccessoryView: UIView {
            get {
                return self.toolbar
            }
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            textField.inputAccessoryView = toolbar
        }
    }
    

    In this case, whenever text field resigns first responder, it defaults first responder to main view. Keep in mind, you might want to explicitly resign first responder, and set main view as first responder if there are multiple UI elements and first responder defaults to undesired view after resignation.

    0 讨论(0)
  • 2020-12-13 10:22

    Adding to @arik's answer, here is the Swift version:

    class ViewController: UIViewController {
    
      @IBOutlet var textField: UITextField!      
    
      // Input Accessory View
      private var inputAccessoryToolbar: UIToolBar?
      override func canBecomeFirstResponder() -> Bool {
        return true
      }
      override var inputAccessoryView: UIView? {
        return inputAccessoryToolbar
      }
    
      override func viewDidLoad() {
        super.viewDidLoad()
        inputAccessoryToolbar = UIToolbar(frame: CGRectMake(0, 0, view.frame.size.width, 50))
        textField.inputAccessoryView = inputAccessoryToolbar
      }
    
      // UITextFieldDelegate
      func textFieldShouldReturn(textField: UITextField) -> Bool {
        becomeFirstResponder()
        return true
      }
    }
    

    Thanks for the clean solution!

    0 讨论(0)
  • 2020-12-13 10:30

    I've ended up with UIToolBar that is not assigned as input accessory view, and slide up and down on UIKeyboardWillShowNotification / UIKeyboardWillHideNotification

    0 讨论(0)
提交回复
热议问题