Using SDK 6.1, Xcode 4.6.1, I make a new project Master-Detail iOS App, ARC, no storyboards.
Then in the DetailViewController
, in the viewDidLoad
I add two UITableView
s contained in UIViewController
s and make sure the second one is hidden like this:
- (void)viewDidLoad
{
[super viewDidLoad];
UIViewController *lViewController1 = [[UIViewController alloc] init];
UITableView *lTableView1 = [[UITableView alloc] initWithFrame: self.view.frame];
lTableView1.scrollsToTop = YES;
[lViewController1.view addSubview: lTableView1];
lTableView1.dataSource = self;
[self.view addSubview: lViewController1.view];
[self addChildViewController: lViewController1];
UIViewController *lViewController2 = [[UIViewController alloc] init];
UITableView *lTableView2 = [[UITableView alloc] initWithFrame: self.view.frame];
lTableView2.scrollsToTop = YES;
[lViewController2.view addSubview: lTableView2];
lTableView2.dataSource = self;
[self.view addSubview: lViewController2.view];
[self addChildViewController: lViewController2];
// now hide the view in view controller 2
lViewController2.view.hidden = YES;
}
(I make sure the DetailViewController
is a datasource that returns 100 rows of UITableViewCell
s with the textLabel.text
set to @"hello"
)
The presence of the second view controller makes that scrollsToTop
(tapping on the status bar) does not work anymore. If I do not use UIViewController
containment and just add two UITableView
s and set the second one to be hidden, scrollsToTop
does work.
What am I doing wrong?
scrollsToTop
only works on a single visible view. From the documentation:You could try calling
[tableView setContentOffset:CGPointZero animated:YES]
on each of your table (or scroll) views manually instead. To do this, implement thescrollViewShouldScrollToTop:
method in theUIScrollViewDelegate
protocol:I have used this and now it works fine.
I am currently experimenting with your project. When
is replaced with
then the scrolling works, even with controller containment.
I tried to insert a view between the controller's view and the table and then hide this view, but the table was not scrolling.
I tried to hide the controller by experimenting with
shouldAutomaticallyForwardAppearanceMethods
but the table was not scrolling.Result: From my experiments, only one scroll view must be visible in the view hierarchy and the
hidden
property of the parent views is not checked out.hidden
must be set toNO
on all other scroll views, not their parent views.You can only set 1 ScrollView per ViewController with property .scrollsToTop = YES. If you set 2 scrollview.scrollsTopTop = YES, it will simply stop functioning.
then, scrollsToTop works correctly. If there are more than 1 scrollview you wish to concurrently setScrollsToTop, keep digging around. good luck!
After testing several options and various hits and try I finally settled to one final solution, i.e.
setBounds:
ofscrollView
(that istableView
in your case) and it works good. You'll have to put extra effort for animation although.By the way in your case, try returning
YES
toAlthough if not defined, assumes YES.