我首先进行了搜索,并且看到了类似的问题,但没有关于如何解决问题的明确答案。我的应用程序的问题是我有3个导航控制器作为子视图控制器添加到UIScrollView中。我最初想要使用UIPageViewController,但事实证明这是一团糟,因为你不能禁用它上面的弹跳。对于每个导航控制器,tableView控制器都嵌入在故事板中。
不会为我的子视图控制器调用viewDidAppear和viewDidDisappear,因为它们的视图在运行时作为子视图添加到分页滚动视图中,我有点相信这就是为什么我无法弄清楚如何解决这个问题。我需要每个表视图控制器响应用户触摸相应tableView的状态栏以滚动到顶部。
以下是我用于设置应用程序主滚动视图并添加3个视图控制器视图的代码:
感谢您提供的任何帮助!
MainViewController.h
#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController <UIScrollViewDelegate>
@end
MainViewController.m:
#import "MainViewController.h"
@interface MainViewController ()
@property (nonatomic, strong) UINavigationController *settings;
@property (nonatomic, strong) UINavigationController *hehTwo;
@property (nonatomic, strong) UINavigationController *hehOne;
@property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
@end
@implementation MainViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.settings = [self.storyboard instantiateViewControllerWithIdentifier:@"Settings"];
self.hehTwo = [self.storyboard instantiateViewControllerWithIdentifier:@"HehTwo"];
self.hehOne = [self.storyboard instantiateViewControllerWithIdentifier:@"HehOne"];
[self addChildViewController:self.settings];
[self addChildViewController:self.hehTwo];
[self addChildViewController:self.hehOne];
CGRect hehTwoFrame = self.hehTwo.view.frame;
hehTwoFrame.origin.x = 320;
self.hehTwo.view.frame = hehTwoFrame;
CGRect hehOneFrame = self.hehOne.view.frame;
hehOneFrame.origin.x = 640;
self.hehOne.view.frame = hehOneFrame;
[self.scrollView addSubview:self.settings.view];
[self.scrollView addSubview:self.hehTwo.view];
[self.scrollView addSubview:self.hehOne.view];
// Setting offset so the farthest right controller is heh One
[self.scrollView setContentOffset:CGPointMake(640,0)];
self.scrollView.delegate = self;
self.scrollView.bounces = NO;
self.scrollView.delaysContentTouches = NO;
self.scrollView.contentSize = CGSizeMake(960, self.view.frame.size.height);
self.scrollView.scrollsToTop = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGFloat width = scrollView.frame.size.width;
NSInteger page = (scrollView.contentOffset.x + (0.5f * width)) / width;
if (page == 0)
{
// Settings
}
else if (page == 1)
{
// Heh Two
}
else
{
// Heh One
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
答案 0 :(得分:3)
首先,您需要在{strong>他们的 self.tableView.scrollsToTop = NO;
上设置HehTwo
上的HehOne
和viewDidLoad
(如果这两个最初是隐藏的)实施
现在,更新此方法:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGFloat width = scrollView.frame.size.width;
NSInteger page = (scrollView.contentOffset.x + (0.5f * width)) / width;
if (page == 0)
{
// Settings
((UITableViewController *)self.settings.viewControllers[0]).tableView.scrollsToTop = YES;
((UITableViewController *)self.hehOne.viewControllers[0]).tableView.scrollsToTop = NO;
((UITableViewController *)self.hehTwo.viewControllers[0]).tableView.scrollsToTop = NO;
}
else if (page == 1)
{
// Heh Two
((UITableViewController *)self.settings.viewControllers[0]).tableView.scrollsToTop = NO;
((UITableViewController *)self.hehOne.viewControllers[0]).tableView.scrollsToTop = NO;
((UITableViewController *)self.hehTwo.viewControllers[0]).tableView.scrollsToTop = YES;
}
else
{
// Heh One
((UITableViewController *)self.settings.viewControllers[0]).tableView.scrollsToTop = NO;
((UITableViewController *)self.hehOne.viewControllers[0]).tableView.scrollsToTop = YES;
((UITableViewController *)self.hehTwo.viewControllers[0]).tableView.scrollsToTop = NO;
}
}
这就是全部!这应该解决它。