ScrollView的iOS AutoLayout问题

Zer*_*ter 2 iphone uiscrollview modalviewcontroller ios autolayout

你好,我有一个自动布局问题UIScrollViewModalViewController.以下是我的编码步骤作为示例代码:

1)我有UIViewController一个UIScrollViewassubView

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]];
Run Code Online (Sandbox Code Playgroud)

这很有效

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];
Run Code Online (Sandbox Code Playgroud)

这项工作也很好

3)我滚动到内容偏移y 180,我只能看到view2

[scrollView setContentOffset:CGPointMake(0, 180) animated:YES];
Run Code Online (Sandbox Code Playgroud)

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];
Run Code Online (Sandbox Code Playgroud)

}

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

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

}];
Run Code Online (Sandbox Code Playgroud)

}

我希望有人可以帮我解决这个问题.

小智 6

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

我甚至尝试使用UIScrollView而不是表视图作为主要的开箱即用的主要细节应用程序,并且能够证明缺陷.

表视图和集合视图似乎没有此问题.