Module: RMXKeyboardHelpers

Included in:
RMXTableViewController, RMXViewController
Defined in:
lib/motion/RMXKeyboardHelpers.rb

Instance Method Summary collapse

Instance Method Details

#keyboard_proxyObject



3
4
5
6
# File 'lib/motion/RMXKeyboardHelpers.rb', line 3

def keyboard_proxy
  keyboard_proxy_constraints unless @keyboard_proxy_constraints
  @keyboard_proxy
end

#keyboard_proxy_constraintsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/motion/RMXKeyboardHelpers.rb', line 8

def keyboard_proxy_constraints
  @keyboard_proxy ||= UIView.new
  @keyboard_proxy_constraints ||= begin
    x = {}
    RMX::Layout.new do |layout|
      layout.view = view
      layout.subviews = {
        "keyboard_proxy" => @keyboard_proxy
      }
      x[:bottom] = layout.eq "keyboard_proxy.bottom == #{-RMX.currentKeyboardHeight}"
      x[:height] = layout.eq "keyboard_proxy.height == 0"
    end
    x
  end
end

#keyboardChanged(info) ⇒ Object

by default, looks to see if the controller is using the @keyboard_proxy_constraint convention. if so, sets the constraint’s constant and refreshes the layout in the same animationDuration as the keyboard’s animation.

if you want to do more/other stuff on keyboardChanged, you can override this, call super, or do everything on your own.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/motion/RMXKeyboardHelpers.rb', line 42

def keyboardChanged(info)
  if constraint = @keyboard_proxy_constraints && @keyboard_proxy_constraints[:height]
    Dispatch::Queue.main.async do
      UIView.animateWithDuration(info[:animationDuration], animations: lambda do
        keyboard_proxy_constraints[:bottom].constant = -RMX.currentKeyboardHeight
        view.setNeedsUpdateConstraints
        view.layoutIfNeeded
      end)
    end
  end
end

#keyboardChangedInternal(notification) ⇒ Object

listens for the rmxKeyboardChanged notification and extracts the userInfo to call a friendlier method



29
30
31
32
33
34
# File 'lib/motion/RMXKeyboardHelpers.rb', line 29

def keyboardChangedInternal(notification)
  if isViewLoaded
    info = notification.userInfo
    keyboardChanged(info)
  end
end

#listenForKeyboardChangedObject



24
25
26
# File 'lib/motion/RMXKeyboardHelpers.rb', line 24

def listenForKeyboardChanged
  NSNotificationCenter.defaultCenter.addObserver(self, selector:'keyboardChangedInternal:', name:"rmxKeyboardChanged", object:nil)
end