2012-09-20 49 views
2

你好,我有一个自动布局问题UIScrollViewModalViewController。 这里是我的编码步骤的示例代码:iOS AutoLayout问题与ScrollView

1)我有一个UIViewControllerUIScrollView作为subView

UIViewController *viewController = [[UIViewController alloc] init]; 
UIScrollView *scrollView = [[UIScrollView alloc] init]; 

scrollView.translatesAutoresizingMaskIntoConstraints = NO; 
[viewController.view addSubview:scrollView]; 

UIView *superView = viewController.view; 
NSDictionary *viewsDict = NSDictionaryOfVariableBindings(superView,scrollView); 

[superView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView(==superView)]|" 
                    options:0 
                    metrics:nil 
                    views:viewsDict]]; 
[superView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView(==superView)]|" 
                    options:0 
                    metrics:nil 
                    views:viewsDict]]; 

这个作品

2)我加2只欣赏到scrollView

UIView *view1 = [[UIView alloc] init]; 
view1.backgroundColor = [UIColor redColor]; 
view1.translatesAutoresizingMaskIntoConstraints = NO; 
[scrollView addSubview:view1]; 


UIView *view2 = [[UIView alloc] init]; 
view2.backgroundColor = [UIColor greenColor]; 
view2.translatesAutoresizingMaskIntoConstraints = NO; 
[scrollView addSubview:view2]; 

viewsDict = NSDictionaryOfVariableBindings(scrollView,view1,view2); 


[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view1(==scrollView)]|" 
                    options:0 
                    metrics:nil 
                    views:viewsDict]]; 
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view2(==scrollView)]|" 
                    options:0 
                    metrics:nil 
                    views:viewsDict]]; 

[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view1(180)][view2(==scrollView)]|" 
                    options:0 
                    metrics:nil 
                    views:viewsDict]]; 


UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[btn setTitle:@"open" forState:UIControlStateNormal]; 
btn.frame = CGRectMake(10, 10, 100, 100); 
[view2 addSubview:btn]; 
[btn addTarget:self action:@selector(openPopOver) forControlEvents:UIControlEventTouchUpInside]; 

这个工程也很好

3)I滚动到内容偏移Y 180,我只能看到视图2

[scrollView setContentOffset:CGPointMake(0, 180) animated:YES]; 

4)我打开一个ModalViewController在`的ViewController

- (void)openModal{ 
UIViewController *viewController = [[UIViewController alloc] init]; 

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[btn setTitle:@"close" forState:UIControlStateNormal]; 
btn.frame = CGRectMake(10, 10, 100, 100); 
[viewController.view addSubview:btn]; 
[btn addTarget:self action:@selector(closePopOver) forControlEvents:UIControlEventTouchUpInside]; 


[self presentViewController:viewController animated:YES completion:nil]; 

}

5)当我关闭ModalViewController我的布局在scrollView没有工作,它滚动到我还没有设置不同的内容偏移量。当我将内容偏移重置为y 180时,滚动视图的内容大小是错误的。

- (void)closeModal{ 
[self dismissViewControllerAnimated:YES completion:^{ 

}]; 

}

我希望有人能帮助我解决这个问题。

回答

3

UIViewControllerpresentViewController:方法的文档:

此方法设置presentedViewController属性来指定的视图控制器,调整大小该视图控制器的视图,然后添加视图到视图层级结构。该视图根据呈现的视图控制器的modalTransitionStyle属性中指定的转换样式在屏幕上进行动画处理。

所以调整你的视图控制器的目的是。好吧,无论如何,由苹果公司。

解决此问题的一种方法是记录调整大小的视图的坐标并推断出确切的内容已经发生了变化。也许你可以通过一种方式来调整你的约束,以防止这种情况。

或者,您可以尝试调整此方法的完成处理程序中的所有内容。

3

也许您可以重置viewWillAppear中的contentoffset? 您是否已经实施了-(void)layoutsubviews

+0

这是我的第一个想法重置viewoff的内容偏移出现,但我将contentoffset设置为y 180它不起作用。 – Zeropointer

+0

(void)layoutSubviews在ViewController中不起作用 – Zeropointer

+0

是的,你说得对,对不起。虽然你可以尝试使用layoutsubviews制作一个自定义的UIView,并且看看它是否能够以这种方式更好地工作。我个人喜欢总是这样做。无论如何,它真的是你展示的popover吗?由于该方法说openPopOver,但我没有看到关于popovercontroller的任何代码? –

6

我认为这可能是iOS自动布局和UIScrollViews中的一个错误。我只有在我的情况下有类似的问题,我会推到一个详细视图,然后回到主视图这是一个UIScrollView。滚动视图中的内容将被上移。将滚动视图滚动到顶部,然后再次推动并弹出,并且事情将被正确重置。

我甚至使用UIScrollView而不是表格视图作为主人,尝试了简单的框外主细节应用程序,并能够证明缺陷。

表视图和集合视图似乎没有这个问题。

+0

我已经看到了完全相同的问题。 – Maurizio

+1

这是iOS中的一个真正的bug:http://openradar.appspot.com/radar?id=2932404 – phatmann