Method: MotionPrime::BaseFieldSection#focus

Defined in:
motion-prime/sections/form/base_field_section.rb

#focus(begin_editing = false) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'motion-prime/sections/form/base_field_section.rb', line 66

def focus(begin_editing = false)
  # focus on text field
  elements.values.detect do |element|
    if element.view.is_a?(UITextField) || element.view.is_a?(UITextView)
      element.view.becomeFirstResponder
    end
  end if begin_editing

  # scroll to cell
  # path = form.collection_view.indexPathForCell(cell)
  # form.collection_view.scrollToRowAtIndexPath path,
  #   atScrollPosition: UITableViewScrollPositionTop, animated: true
  return self unless input_view = element(:input).try(:view)
  origin = input_view.frame.origin
  point = container_view.convertPoint(origin, toView: form.collection_view)

  nav_bar_height = screen.navigationController ? screen.navigationController.navigationBar.frame.size.height : 0
  nav_bar_height += 20 # status bar height

  offset = form.collection_view.contentOffset
  new_top_offset = point.y - nav_bar_height
  unless new_top_offset == offset.y
    offset.y = new_top_offset
    form.collection_view.setContentOffset(offset, animated: true)
  end

  self
rescue
  NSLog("can't focus on element #{self.class_name_without_kvo}")
end