嵌入容器视图到UIScrollView的

嵌入容器视图到UIScrollView的

问题描述:

我一直在寻找高和低,试图弄清楚这一点,希望有人能帮助(我想这大概是相当微不足道,但我只是在混乱的情况!)嵌入容器视图到UIScrollView的

我使用XCode 5和故事板。我有一个容器视图,占据了iPhone屏幕空间的75%左右。在这之上我有一些按钮(和其他小工具)。我希望能够在按下其中一个按钮时将视图控制器加载到容器中。

到目前为止,所有这些工作正常,但正在加载的视图控制器大小不同,并且将来可能会更改。我希望能够允许(任意大小的)子视图滚动。

我已经尝试在滚动视图中嵌入容器,但随后作为添加新子视图的过程的一部分,您必须设置框架大小,但我希望孩子弄清楚它应该有多大然后设置一切。

我不完全知道什么是最好的方式去了解这是(在故事板设置的限制,或以编程方式?)

任何意见将是巨大的!或者指向一个教程的链接将会非常有用,我唯一可以找到的就是将视图控制器添加到容器视图或将视图添加到滚动视图中,而这些视图在Xcode 5(iOS 7)中似乎没有像预期的那样工作,

谢谢!

CNC中

这是我用来展示任意大小的详细视图控制器的代码,其余的一切都在故事板设置(以防万一,这是在未来的有用的人!)这现在的作品:

- (void)presentDetailController:(UIViewController*)detailVC{ 

//0. Remove the current Detail View Controller shown 
if(self.currentDetailViewController){ 
    [self removeCurrentDetailViewController]; 
} 

//1. Add the detail controller as child of the container 
[self addChildViewController:detailVC]; 

//2. DO NOT Define the detail controller's view size 
//detailVC.view.frame = [self frameForDetailController]; 

//3. Add the Detail controller's view to the Container's detail view and save a reference to the detail View Controller 
[self.detailView addSubview:detailVC.view]; 
self.currentDetailViewController = detailVC; 

// Pin the height to the container to make it scrollable? 
[self.detailView addConstraint:[NSLayoutConstraint constraintWithItem:self.detailView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:detailVC.view.bounds.size.height]]; 

//4. Complete the add flow calling the function didMoveToParentViewController 
[detailVC didMoveToParentViewController:self]; 

} 

您可以在任何地方设置约束并使其正常工作。秘诀是:您必须将容器视图的约束设置为包含它的滚动视图的顶部,左侧,底部和右侧。然后Autolayout将根据容器视图内部的内容正确计算滚动视图的内容大小。

请参阅iOS 6发行说明中标题为Pure Auto Layout Approach的小节。甚至有一些示例代码。

// parentView is the thing you want to add the scroll view to 


// setup the scroll view, and add it to the parent view 
UIScrollView* scrollView = [[UIScrollView alloc] init]; 
scrollView.backgroundColor = [UIColor colorWithWhite:0.9f alpha:1.0f]; 

[parentView addSubview:scrollView]; 

scrollView.translatesAutoresizingMaskIntoConstraints = NO; 

NSDictionary* views = NSDictionaryOfVariableBindings(scrollView); 

[parentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics:nil views:views]]; 
[parentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics:nil views:views]]; 


// set up the content 
UILabel* firstLongLabel = [[UILabel alloc] init]; 
firstLongLabel.numberOfLines = 0; 
firstLongLabel.text = @" Four score and seven years ago our fathers brought forth, on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.\n\nNow we are engaged in a great civil war, testing whether that nation, or any nation so conceived, and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting-place for those who here gave their lives, that that nation might live. It is altogether fitting and proper that we should do this."; 
firstLongLabel.backgroundColor = [UIColor greenColor]; 

UIButton* middleButton = [UIButton buttonWithType:UIButtonTypeSystem]; 
[middleButton setTitle:@"Middle Button" forState:UIControlStateNormal]; 
middleButton.backgroundColor = [UIColor purpleColor]; 

UILabel* lastLongLabel = [[UILabel alloc] init]; 
lastLongLabel.numberOfLines = 0; 
lastLongLabel.text = @"But, in a larger sense, we can not dedicate, we can not consecrate – we can not hallow – this ground. The brave men, living and dead, who struggled here, have consecrated it far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us – that from these honored dead we take increased devotion to that cause for which they here gave the last full measure of devotion - that we here highly resolve that these dead shall not have died in vain – that this nation, under God, shall have a new birth of freedom, and that government of the people, by the people, for the people, shall not perish from the earth."; 
lastLongLabel.backgroundColor = [UIColor yellowColor]; 


// now create a content view and add all of our previously created content to it 
UIView* contentView = [[UIView alloc] init]; 
contentView.backgroundColor = [UIColor blueColor]; 

[contentView addSubview:firstLongLabel]; 
[contentView addSubview:middleButton]; 
[contentView addSubview:lastLongLabel]; 

[scrollView addSubview:contentView]; 


// and now comes the important part.. the constraints 
firstLongLabel.translatesAutoresizingMaskIntoConstraints = middleButton.translatesAutoresizingMaskIntoConstraints = lastLongLabel.translatesAutoresizingMaskIntoConstraints = contentView.translatesAutoresizingMaskIntoConstraints = NO; 

views = NSDictionaryOfVariableBindings(firstLongLabel, middleButton, lastLongLabel, contentView); 

// first, add constraints dealing with the content inside the content view 
// these are simple and should be easily understood 
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[firstLongLabel]-10-|" options:0 metrics:nil views:views]]; 
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[middleButton]-10-|" options:0 metrics:nil views:views]]; 
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[lastLongLabel]-10-|" options:0 metrics:nil views:views]]; 

[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[firstLongLabel]-10-[middleButton]-10-[lastLongLabel]-10-|" options:0 metrics:nil views:views]]; 

// then add constraints dealing with the content view that we put into the scroll view... 
// the trick here is that we lock the content to only partially the width 
// of the scroll view's parent. if we don't do that, then the labels will force the scroll 
// view to expand horizontally instead of vertically. 
// 
// as an exercise, you can try locking the left and right to the scroll view instead of the parent view to see what happens 
[parentView addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeLeft multiplier:1 constant:10]]; 
[parentView addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeRight multiplier:1 constant:-10]]; 

// by locking the content view's vertical edges to that of the scroll view, it'll cause the 
// scroll view to expand vertically to accomodate the entire content view 
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[contentView]-10-|" options:0 metrics:nil views:views]]; 
+0

谢谢你,我已经试过这种方法,但无法让它工作(滚动视图只是不滚动)。在加载新视图控制器视图时,如何设置容器和子视图的框架? – user2851943

+0

你让自动布局为你做。我会尽量整理一个例子。 – Travis

+0

一个例子会很棒!非常感谢! – user2851943