具有2个子视图的UIScrollView的自动布局

时间:2015-08-19 15:36:02

标签: ios objective-c uiscrollview xcode6 autolayout

我有一个滚动视图,它在故事板中设置了自动布局约束,以便填充整个视图。滚动视图包含由nibs实例化的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+模拟器中,滚动视图的宽度和高度都比窗口小,因此右侧和底部都有白色空间。

在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(将边缘固定到superview)

1 个答案:

答案 0 :(得分:0)

将您的滚动视图从左侧,右侧,顶部和底部绑定,然后它看起来很好。enter image description here

它看起来像..

enter image description here

这是我和你一起写的代码

  • (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;

}