iOS的自动版式问题与滚动型 [英] iOS AutoLayout issue with ScrollView

查看:106
本文介绍了iOS的自动版式问题与滚动型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有的UIScrollView 自动布局问题, ModalViewController
下面是我的编码步骤为样本code:

Hello i have an auto layout issue with UIScrollView and ModalViewController. Here are my coding steps as sample code:

1)我有一个的UIViewController 的UIScrollView 子视图

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分欣赏到滚动视图

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,我只能看到视图2

3) I scroll to content offset y 180 that i can only see the 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 我的布局,它滚动到一个不同内容偏移,我没有设定。当我重置内容偏移为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.

推荐答案

从<一个文档href=\"http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html\"相对=nofollow> 的UIViewController presentViewController:方法:

此方法设置presentedViewController属性指定的视图控制器,调整大小,该视图控制器的视图,然后添加视图视图层次。视图是根据在presented视图控制器的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.

去了解一种方法是登录该调整视图的坐标,并推断究竟是什么已经改变。也许你可以在某种程度上prevent这种调整的约束。

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.

这篇关于iOS的自动版式问题与滚动型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆