ios - Getting tableView to scroll as one with parent scrollView after being added with addChildViewController -


i'm working through tutorial on child view controllers in rubymotion. (what i'm trying create tabbed interface in likes of instagram's profile pages.) have things working, problem container has scrollview , child i'm adding tableview, scrollview. 2 scroll independent of each other, i'd them scroll one. there way can given code below?

my container:

class profilecontroller < sharedcontroller   def viewdidload     super      scroll_frame = self.view.bounds      @scroll = uiscrollview.alloc.initwithframe(scroll_frame)     @scroll.bounces = true     @scroll.delegate = self     @scroll.alwaysbouncevertical = true     @scroll.contentsize = cgsizemake(uiscreen.mainscreen.bounds.size.width, scroll_frame.size.height)      main_container = uiviewcontroller.alloc.init     self.push(main_container)   end    def tap_child(sender)     set_active_tab(sender)     controller             = childpartialcontroller.alloc.initwithnibname(nil, bundle:nil)     controller.data        = @data[:child]     controller.parent      = self     controller.scroll_view = @scroll     self.push(controller)   end end 

the child:

class childpartialcontroller < sharedcontroller   attr_accessor :data, :scroll_view, :parent    def viewdidappear(animated)     scroll_view.contentsize = cgsizemake(uiscreen.mainscreen.bounds.size.width, 272)   end    def viewdidload     super      self.title = "child"      @data = data      @table = uitableview.alloc.initwithframe(       cgrectmake(0, parent.tab_bar.frame.origin.y + parent.tab_bar.frame.size.height, self.view.bounds.size.width, self.view.bounds.size.height),       style: uitableviewstylegrouped     )     @table.autoresizingmask = uiviewautoresizingflexibleheight     @table.rowheight        = 30      parent.view.addsubview(@table)      @table.datasource = self     @table.delegate = self   end end 

sharedcode:

class sharedcontroller < uiviewcontroller   def push(to_vc, animated = false)     from_vc = self.stack[-1] # nil      self.stack.pop if self.stack.count > 1      self.stack.push(to_vc)      self.addchildviewcontroller(to_vc)      post_animate = lambda { |finished|       to_vc.didmovetoparentviewcontroller(self)       to_vc.viewdidappear(animated)     }      if !from_vc.nil?       to_vc.view.frame = child_frame       to_vc.viewwillappear(animated)     end      @scroll.addsubview(to_vc.view)     post_animate.call(true)      p self.stack.count   end    def child_height     self.view.bounds.size.height - 232   end    def child_width     self.view.bounds.size.width   end    def child_frame     cgrectmake(0, self.tab_bar.frame.origin.y + self.tab_bar.frame.size.height + 10, child_width, child_height)   end    def offscreen_top_frame     cgrectmake(0, 50 - child_height, child_width, child_height)   end    def offscreen_bottom_frame     cgrectmake(0, self.view.bounds.size.height, child_width, child_height)   end end 

as turns out think making things way complicated approach. instead, turned child view controllers subclasses of uiview, added uitableview that, added uiview parent's scroll view subview. here's example in case interested:

my container:

class profilecontroller < sharedcontroller   def viewdidload     super      scroll_frame = self.view.bounds      @scroll = uiscrollview.alloc.initwithframe(scroll_frame)     @scroll.bounces = true     @scroll.delegate = self     @scroll.alwaysbouncevertical = true     @scroll.contentsize = cgsizemake(uiscreen.mainscreen.bounds.size.width, scroll_frame.size.height)   end    def tap_child(sender)     set_active_tab(sender)     controller             = childpartial.alloc.initwithframe(cgrectmake(0, self.tab_bar.frame.origin.y + self.tab_bar.frame.size.height, self.view.bounds.size.width, self.view.bounds.size.height))     controller.data        = @data[:child]     controller.parent      = self     controller.scroll_view = @scroll     controller.build_table     @scroll.addsubview(controller)   end end 

child:

class childpartial < uiview   attr_accessor :data, :parent, :scroll_view    def initwithframe(frame)     super      self   end    def data=(data)     @data = data   end    def parent=(parent)     @parent = parent   end    def scroll_view=(scroll_view)     @scroll_view = scroll_view   end    def build_table     @table = uitableview.alloc.initwithframe(self.bounds, style: uitableviewstylegrouped)     @table.autoresizingmask = uiviewautoresizingflexibleheight     @table.userinteractionenabled = false      @table.datasource = self     @table.delegate = self      resize_scroll_view      self.addsubview(@table)   end    def resize_scroll_view     @scroll_view.contentsize = cgsizemake(parent.view.bounds.size.width, parent.view.bounds.size.height + @table.bounds.size.height)   end  end 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -