【问题标题】:Auto Layout for UIScrollView w/ 2 Child Views带有 2 个子视图的 UIScrollView 的自动布局
【发布时间】:2015-11-13 00:22:43
【问题描述】:

我有一个滚动视图,它设置在带有自动布局约束的情节提要内,以便填充整个视图。滚动视图包含两个由从 nib 实例化的 UIViewControllers 管理的视图。包含这些子视图的代码:

- (void)viewDidLoad {
[super viewDidLoad];
ContentViewController *contentVC = [ContentViewController new];
[[NSBundle mainBundle] loadNibNamed:@"ContentView" owner:contentVC options:nil];

self.scrollView.delegate = self;
[self addChildViewController:contentVC];
[self.scrollView addSubview:contentVC.contentTC.view];
[contentVC didMoveToParentViewController:self];
[self addChildViewController:contentVC.contentTC];

self.view.layer.borderColor = [[UIColor blackColor] CGColor];
self.view.layer.borderWidth = 5.0;
self.scrollView.layer.borderColor = [[UIColor blueColor] CGColor];
self.scrollView.layer.borderWidth = 5.0;
CGRect adminFrame = contentVC.view.frame;
adminFrame.origin.x = adminFrame.size.width;

ShareViewController *shareViewController = [[ShareViewController alloc] initWithNibName:@"ShareView" bundle:nil];
[self addChildViewController:shareViewController];
[self.scrollView addSubview:shareViewController.view];

shareViewController.view.frame = adminFrame;


CGRect shareFrame = shareViewController.view.frame;
shareFrame.origin.x = shareFrame.size.width;

[self.view addSubview:self.scrollView];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView
                                                       attribute:NSLayoutAttributeRight
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:self.view
                                                       attribute:NSLayoutAttributeRight
                                                      multiplier:1.0
                                                        constant:0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView
                                                       attribute:NSLayoutAttributeBottom
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:self.view
                                                       attribute:NSLayoutAttributeBottom
                                                      multiplier:1.0
                                                        constant:0]];


// 4) Finally set the size of the scroll view that contains the frames
CGFloat scrollWidth  = 2 * self.view.frame.size.width;
CGFloat scrollHeight  = self.view.frame.size.height;
self.scrollView.contentSize = CGSizeMake(scrollWidth, scrollHeight);
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.bounces = NO;

没有能力发布图片,但这里有一个链接到它的样子:

dropbox.com/s/e84ya3ffrdzytac/Screenshot%202015-08-19%2011.52.15.png

在iphone 6+模拟器中,scrollview的宽度和高度都比window小,所以右边和底部都有空白。

在 iPhone 4S 模拟器中,宽度是正确的,但是子视图比窗口长,这意味着它们会向下滚动。这仅适用于 iPhone 5,即使所有视图都是推断出来的,并且没有针对 4 英寸屏幕进行设置。

抱歉,如果这是一个多余的问题,但我一直在搜索 SO 并将我的头撞到墙上两天了,我们将不胜感激。

** 编辑:更新的代码和行为:**

- (void)viewDidLoad {
[super viewDidLoad];
ContentViewController *contentVC = [ContentViewController new];
[[NSBundle mainBundle] loadNibNamed:@"ContentView" owner:contentVC options:nil];

self.scrollView.delegate = self;
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.bounces = NO;
self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;

[self.view addSubview:self.scrollView];

UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width * 2, self.view.frame.size.height)];
[self.scrollView addSubview:contentView];

[self addChildViewController:contentVC];
[contentView addSubview:contentVC.contentTC.view];
[contentVC didMoveToParentViewController:self];
[self addChildViewController:contentVC.contentTC];

CGRect adminFrame = contentVC.view.frame;
adminFrame.origin.x = adminFrame.size.width;


ShareViewController *shareViewController = [[ShareViewController alloc] initWithNibName:@"ShareView" bundle:nil];
[self addChildViewController:shareViewController];
[contentView addSubview:shareViewController.view];

shareViewController.view.frame = adminFrame;

self.view.layer.borderColor = [[UIColor blackColor] CGColor];
self.view.layer.borderWidth = 3;

self.scrollView.layer.borderColor = [[UIColor blueColor] CGColor];
self.scrollView.layer.borderWidth = 5;

contentView.layer.borderColor = [[UIColor greenColor] CGColor];
contentView.layer.borderWidth = 7;


CGRect shareFrame = shareViewController.view.frame;
shareFrame.origin.x = shareFrame.size.width;

[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[v]|" options:0 metrics:nil views:@{@"v" : contentView}]];
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[v]|" options:0 metrics:nil views:@{@"v" : contentView}]];

结果:dropbox.com/s/29zwwn6inmdvz0e/Screenshot%202015-08-19%2014.27.47.png?dl=0 , dropbox.com/s/rqsdk9yubq84ph6/Screenshot%202015-08-19%2014.28.14 .png?dl=0

这是来自故事板的约束图像:dropbox.com/s/8c5wkfw81sqd8ij/Screenshot%202015-08-19%2014.28.47.png?dl=0(将边缘固定到超级视图)

【问题讨论】:

  • 问题是什么?你已经描述了会发生什么,但你没有说出你认为的地方。
  • 抱歉,我认为很清楚 - “这仅适用于 iPhone 5” - 我如何更改它以使其适用于 4 和 6?
  • 目前还不清楚。 - 你了解自动布局如何与滚动视图一起使用吗?
  • 什么不清楚?从图片和描述来看,上面的代码仅在 iphone 5 屏幕中呈现了所需的行为,而不是在 iphone 4 或 6 中。显然,如果解决方案很明显,我不能理解自动布局如何使用滚动视图工作,而我不是看到了。
  • 嗯,很难说从哪里开始。这里没有人可以看到您的故事板内部,但显然您做错了。例如,您说:“我有一个滚动视图,它设置在带有自动布局约束的情节提要内,以便填充整个视图”。但这显然是错误的,因为您说(并且图片显然试图显示)在 iPhone 6+ 上,滚动视图不会填满整个视图。因此,您甚至没有正确定位滚动视图的基本约束。在滚动视图位置正确之前,您无法获得正确的内容约束。

标签: ios objective-c uiscrollview xcode6 autolayout


【解决方案1】:

从左、右、上和下绑定你的滚动视图然后它看起来很好。

它看起来像..

这是我用你写的代码

  • (void)viewDidLoad

{

[super viewDidLoad];

UIViewController *contentVC = [[UIViewController alloc]init];
contentVC.view.backgroundColor = [UIColor greenColor];
self.scrollView.delegate = self;
[self addChildViewController:contentVC];
[self.scrollView addSubview:contentVC.view];
[contentVC didMoveToParentViewController:self];
[self addChildViewController:contentVC];

self.view.layer.borderColor = [[UIColor blackColor] CGColor];
self.view.layer.borderWidth = 5.0;
self.scrollView.layer.borderColor = [[UIColor blueColor] CGColor];
self.scrollView.layer.borderWidth = 5.0;
CGRect adminFrame = contentVC.view.frame;
adminFrame.origin.x = adminFrame.size.width;

UIViewController *shareViewController = [[UIViewController alloc] init];
shareViewController.view.backgroundColor = [UIColor grayColor];
[self addChildViewController:shareViewController];
[self.scrollView addSubview:shareViewController.view];

shareViewController.view.frame = adminFrame;


CGRect shareFrame = shareViewController.view.frame;
shareFrame.origin.x = shareFrame.size.width;

[self.view addSubview:self.scrollView];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView
                                                      attribute:NSLayoutAttributeRight
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeRight
                                                     multiplier:1.0
                                                       constant:0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView
                                                      attribute:NSLayoutAttributeBottom
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeBottom
                                                     multiplier:1.0
                                                       constant:0]];


// 4) Finally set the size of the scroll view that contains the frames
CGFloat scrollWidth  = 2 * self.view.frame.size.width;
CGFloat scrollHeight  = self.view.frame.size.height;
self.scrollView.contentSize = CGSizeMake(scrollWidth, scrollHeight);
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.bounces = NO;

}

【讨论】:

  • 抱歉,我认为这不是一个好的解决方案。这些约束已经存在于我的故事板中。
  • 此图像代码是您在我这边添加这些约束后运行良好的代码。
  • 我真的不认为你在运行我的代码 - 你错过了所有带有自动布局约束的笔尖。
  • 是的,你是对的,我没有添加你的自定义 viewControllers 我正在使用 UIVIewControllers 添加为 ChildViewController。查看我编辑的答案
猜你喜欢
  • 1970-01-01
  • 2015-11-30
  • 1970-01-01
  • 2013-05-25
  • 1970-01-01
  • 2017-07-13
  • 2013-05-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多