我遇到了ChildViewController視圖的問題,它們的框架會自動更改爲其父框架。我已經用一個類將它提煉成一個示例項目來說明發生了什麼。UIScrollView子視圖容器錯誤地更改框架
概括地說,我:
- 有ScrollViewController
- 添加ScrollViewController爲RootViewController的,並添加到窗口
- 在ScrollViewController - 在viewDidLoad中我創建了一個集裝箱的UIView與框架{0,0},{72704,768}}
- 創建一個帶有{{0,0},{1024,768}}的幀的UIScrollView}
- 添加滾動型的ScrollViewController查看
- 添加容器的滾動型
- 用的幀創建71個UIViewControllers {{0,0},{1024,768}}
- 加入他們作爲的ScrollViewController
- ChildViewControllers其子視圖添加到容器
- 日誌中的所有71幀上ViewDidAppear
- 每一幀都是{{0,0},{72704,768}} - 不是1024×768,我設置。
所有自動調整遮罩/自動調整子視圖設置爲NO。
這是在iOS 6上運行,但也發生在iOS 5.1上!
更新! 好像addChildViewController是不知何故的罪魁禍首。不知道爲什麼 - 這是預期的行爲?我需要能夠將這些childViewControllers添加到ScrollViewController,而不會將其視圖框架自動縮放到父母的視圖框架。理論上這不是做這件事的方法嗎?
此外 - 沒有NIB在任何地方。在Scrollview/ContainerView和71個VC視圖中的每一個上設置「translatesAutoresizingMaskIntoConstraints」爲NO。不幸的是同樣的事情。這似乎是很奇怪的行爲...
鏈接項目 - https://dl.dropbox.com/u/637000/TestingBug.zip
代碼ScrollViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
[self initScrollView];
[self createTestViews];
}
- (void)initScrollView {
if(scrollView == nil) {
containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 72704, 768)];
containerView.autoresizingMask = UIViewAutoresizingNone;
containerView.autoresizesSubviews = NO;
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
scrollView.autoresizingMask = UIViewAutoresizingNone;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.autoresizesSubviews = NO;
scrollView.delegate = self;
[self.view addSubview:scrollView];
[scrollView addSubview:containerView];
}
}
- (void)createTestViews {
for(int count = 0; count < 71; count++) {
[self createTestView];
}
}
- (void)createTestView {
UIViewController *testVC = [[UIViewController alloc] init];
testVC.view.frame = CGRectMake(0, 0, 1024, 768);
[self addChildViewController:testVC];
[containerView addSubview:testVC.view];
[testVC didMoveToParentViewController:self];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
for(UIView *subview in containerView.subviews) {
// THIS IS {{0, 0}, {72704, 768}} NOT 1024 x 768
NSLog(@"BROKEN FRAME = %@", NSStringFromCGRect(subview.frame));
}
}
正是我需要的! – chrs