你好,我有一个自动布局问题UIScrollView
和ModalViewController
。 这里是我的编码步骤的示例代码:iOS AutoLayout问题与ScrollView
1)我有一个UIViewController
与UIScrollView
作为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:^{
}];
}
我希望有人能帮助我解决这个问题。
这是我的第一个想法重置viewoff的内容偏移出现,但我将contentoffset设置为y 180它不起作用。 – Zeropointer
(void)layoutSubviews在ViewController中不起作用 – Zeropointer
是的,你说得对,对不起。虽然你可以尝试使用layoutsubviews制作一个自定义的UIView,并且看看它是否能够以这种方式更好地工作。我个人喜欢总是这样做。无论如何,它真的是你展示的popover吗?由于该方法说openPopOver,但我没有看到关于popovercontroller的任何代码? –