iOS的AutoLayout问题与ScrollView
你好,我有一个与UIScrollView
和ModalViewController
的自动布局问题。 这是我的编码步骤作为示例代码:
1)我有一个UIScrollView
作为子subView
的UIViewController
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)我滚动到内容偏移Y 180,我只能看到view2
[scrollView setContentOffset:CGPointMake(0, 180) animated:YES];
4)我在ViewController上打开一个ModalViewController
- (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)当我closuresModalViewController
我的布局在scrollView
没有工作,它滚动到一个不同的内容偏移,我还没有设置。 当我重置内容偏移到Y 180滚动视图的内容大小是错误的。
- (void)closeModal{ [self dismissViewControllerAnimated:YES completion:^{ }];
}
我希望有人能帮我解决这个问题。
从UIViewController
的presentViewController:
方法的文档:
此方法将presentViewController属性设置为指定的视图控制器, 调整视图控制器视图的大小,然后将视图添加到视图层次结构中。 该视图根据呈现的视图控制器的modalTransitionStyle属性中指定的转换样式在屏幕上进行animation处理。
所以调整您的视图控制器的目的是。 那么,无论如何由苹果。
一种解决这个问题的方法是loggingresize的视图的坐标,并推断到底发生了什么变化。 也许你可以调整你的约束,以防止这种情况。
或者,您可以尝试调整此方法的完成处理程序中的所有内容。
我认为这可能是iOS自动布局的UIScrollViews的一个错误。 我只有在我的情况下有类似的问题,我会推到一个细节视图,然后回到主视图这是一个UIScrollView。 滚动视图中的内容将被上移。 将滚动视图滚动到顶部,然后再次推动和popup,事情将被正确重置。
我甚至用UIScrollView而不是表格视图作为主要的东西,试过简单的东西,并能够certificate这个缺陷。
表视图和集合视图似乎没有这个问题。
也许你可以重置viewWillAppear
的contentoffset? 你有没有实现-(void)layoutsubviews
?