您好,我遇到了 UIScrollView
和 ModalViewController
的自动布局问题.以下是我的编码步骤作为示例代码:
Hello i have an auto layout issue with UIScrollView
and ModalViewController
.
Here are my coding steps as sample code:
1) 我有一个 UIViewController
和一个 UIScrollView
作为 subView
1) I have an UIViewController
with an UIScrollView
as 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
2) i add 2 sub views to the 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
3) I scroll to content offset y 180 that i can only see the view2
[scrollView setContentOffset:CGPointMake(0, 180) animated:YES];
4) 我在 `ViewController
4) I open a ModalViewController
on the `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 时,滚动视图的内容大小是错误的.
5) when i close the ModalViewController
my Layout on the scrollView
didn't work it scrolls to an different content offset that i have not set. When i reset the content offset to y 180 the content size of the scrollview is wrong.
- (void)closeModal{
[self dismissViewControllerAnimated:YES completion:^{
}];
}
希望有人能帮我解决这个问题.
I hope someone can help me to fix this problem.
来自UIViewController
的 presentViewController:
方法:
此方法将presentedViewController 属性设置为指定的视图控制器,调整该视图控制器视图的大小,然后将该视图添加到视图层次结构中.视图根据呈现的视图控制器的 modalTransitionStyle 属性中指定的过渡样式在屏幕上进行动画处理.
This method sets the presentedViewController property to the specified view controller, resizes that view controller’s view and then adds the view to the view hierarchy. The view is animated onscreen according to the transition style specified in the modalTransitionStyle property of the presented view controller.
因此调整视图控制器的大小是有意的.好吧,无论如何,苹果公司.
So the resizing of your view controller is intended. Well, by Apple, anyway.
解决此问题的一种方法是记录调整大小的视图的坐标并推断出究竟发生了什么变化.也许您可以调整约束以防止这种情况发生.
One way to go about this is to log the resized view's coordinates and infer what exactly has been changed. Maybe you can adjust your constraints in a way to prevent this.
或者,您可以尝试在此方法的完成处理程序中调整所有内容的大小.
Alternatively, you could try resizing everything in the completion handler of this method.
这篇关于ScrollView 的 iOS 自动布局问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!