我正在阅读有关 RubyMotion 中子视图控制器的教程。(我想做的是在 Instagram 的个人资料页面中创建一个选项卡式界面。)我的工作正常,但我的问题是我的容器有一个滚动视图,而我添加的孩子是一个 tableView,它是本身就是一个滚动视图。这两个滚动相互独立,我希望它们作为一个滚动。鉴于下面的代码,有没有办法可以做到这一点?
我的容器:
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
孩子:
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
共享代码:
class SharedController < UIViewController
def push(to_vc, animated = false)
from_vc = self.stack[-1] # could be 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