【问题标题】:iOS AutoLayout issue with ScrollViewScrollView 的 iOS 自动布局问题
【发布时间】:2012-09-12 14:54:19
【问题描述】:

您好,我遇到了 UIScrollViewModalViewController 的自动布局问题。 以下是我的编码步骤作为示例代码:

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) 我滚动到内容偏移 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) 当我关闭ModalViewController 我在scrollView 上的布局不起作用时,它会滚动到我尚未设置的不同内容偏移量。当我将内容偏移量重置为 y 180 时,滚动视图的内容大小是错误的。

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

}];

}

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

【问题讨论】:

    标签: iphone ios uiscrollview modalviewcontroller autolayout


    【解决方案1】:

    我认为这可能是 iOS 自动布局 wrt UIScrollViews 中的错误。仅在我的情况下我遇到了类似的问题,我会推送到详细视图,然后弹回作为 UIScrollView 的主视图。滚动视图中的内容将向上移动。将滚动视图滚动到顶部,然后再次推送和弹出,一切都会正确重置。

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

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

    【讨论】:

    • 我也遇到过同样的问题。
    【解决方案2】:

    来自UIViewControllerpresentViewController:方法的文档:

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

    因此调整视图控制器的大小是有意的。好吧,无论如何,苹果公司。

    解决此问题的一种方法是记录调整大小的视图的坐标并推断出究竟发生了什么变化。也许您可以调整约束以防止这种情况发生。

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

    【讨论】:

      【解决方案3】:

      也许您可以重置viewWillAppear 中的内容偏移量? 你是否已经实现了-(void)layoutsubviews

      【讨论】:

      • 这是我在 viewWill Appear 中重置 contentoffset 的第一个想法,但我将 contentoffset 设置为 y 180 它不起作用。
      • 是的,你是对的,很抱歉。尽管您可以尝试使用 layoutsubviews 制作自定义 UIView 并查看它是否以这种方式更好地工作。我个人喜欢总是这样做。无论如何,这真的是你展示的popover吗?因为方法说 openPopOver 但我没有看到任何关于 popovercontroller 的代码?
      • 不抱歉,这是我用复制粘贴编写的示例代码,方法名称错误
      • 所以我重命名了方法
      • 所以我找到了一个折衷的解决方案。当调用 viewController didDisappear 时,我将 scrollView.contentOffset 设置为 y 0,当 viewController viewDidAppear 我将 contentOffset 设置回 y 180 时
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-21
      相关资源
      最近更新 更多