【发布时间】:2014-02-10 10:38:03
【问题描述】:
我首先进行了搜索,并且看到了一些类似的问题,但没有关于如何解决它的明确答案。我的应用程序遇到的问题是我有 3 个导航控制器作为子视图控制器添加到 UIScrollView 中。我原本想改用 UIPageViewController,但结果是一团糟,因为你不能禁用它的弹跳。对于每个导航控制器,在故事板中都嵌入了一个 tableView 控制器。
没有为我的子视图控制器调用 viewDidAppear 和 viewDidDisappear,因为它们的视图在运行时作为子视图添加到分页滚动视图中,我相信这就是为什么我无法弄清楚如何解决这个问题。
我需要我的每个表格视图控制器来响应用户触摸状态栏以使相应的表格视图滚动到顶部。
这是我用于设置应用程序的主滚动视图并将 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
【问题讨论】:
标签: ios objective-c uiviewcontroller ios7 uiscrollview