ios - 将容器 View 嵌入到 UIScrollView
<p><p>我一直在寻找高低试图找出这个希望有人能提供帮助(我认为这可能相当微不足道,但我只是过度混淆了情况!)</p>
<p>我正在使用 XCode 5 和 Storyboard。我有一个容器 View ,它占据了 iPhone 上大约 75% 的屏幕空间。在此之上,我有一些按钮(和其他小部件)。我希望能够在按下其中一个按钮时将 ViewController 加载到容器中。</p>
<p>到目前为止,所有这些都可以正常工作,但是正在加载的 ViewController 大小不同,并且将来可能会发生变化。我希望能够允许 subview (任意大小)滚动。</p>
<p>我尝试将容器嵌入到 ScrollView 中,但是作为添加新 subview 过程的一部分,您必须设置框架大小,但我希望 child 弄清楚它应该有多大然后相应地设置所有内容。</p>
<p>我不完全确定最好的解决方法是什么(在 Storyboard中设置约束,还是以编程方式?)</p>
<p>任何建议都会很棒!或者教程的链接将非常有用,我能找到的唯一的是将 ViewController 添加到容器 View 或将 View 添加到 ScrollView ,这些似乎都不像 Xcode 5 (iOS 7) 的预期那样工作</p>
<p>谢谢!</p>
<p><strong>-编辑-</strong></p>
<p>这是我用来展示任意大小的细节 ViewController 的代码,其余的都设置在 Storyboard 中(以防万一这对将来的任何人有用!)现在可以了:</p>
<pre><code>- (void)presentDetailController:(UIViewController*)detailVC{
//0. Remove the current Detail View Controller shown
if(self.currentDetailViewController){
;
}
//1. Add the detail controller as child of the container
;
//2. DO NOT Define the detail controller's view size
//detailVC.view.frame = ;
//3. Add the Detail controller's view to the Container's detail view and save a reference to the detail View Controller
;
self.currentDetailViewController = detailVC;
// Pin the height to the container to make it scrollable?
];
//4. Complete the add flow calling the function didMoveToParentViewController
;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您可以在任一位置设置约束并使其正常工作。秘诀是:您必须将容器 View 的约束设置为包含它的 ScrollView 的顶部、左侧、底部和右侧。然后,自动布局将根据容器 View 内部的内容正确计算 ScrollView 的内容大小。</p>
<p>参见标题为 <a href="https://developer.apple.com/library/ios/releasenotes/General/RN-iOSSDK-6_0/index.html" rel="noreferrer noopener nofollow">Pure Auto Layout Approach</a> 的部分在 iOS 6 发行说明中。甚至还有一些示例代码。</p>
<pre><code>// 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 = [ init];
scrollView.backgroundColor = ;
;
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary* views = NSDictionaryOfVariableBindings(scrollView);
|" options:0 metrics:nil views:views]];
|" options:0 metrics:nil views:views]];
// set up the content
UILabel* firstLongLabel = [ 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 = ;
UIButton* middleButton = ;
;
middleButton.backgroundColor = ;
UILabel* lastLongLabel = [ 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 = ;
// now create a content view and add all of our previously created content to it
UIView* contentView = [ init];
contentView.backgroundColor = ;
;
;
;
;
// 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
-10-|" options:0 metrics:nil views:views]];
-10-|" options:0 metrics:nil views:views]];
-10-|" options:0 metrics:nil views:views]];
-10--10--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
];
];
// 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
-10-|" options:0 metrics:nil views:views]];
</code></pre></p>
<p style="font-size: 20px;">关于ios - 将容器 View 嵌入到 UIScrollView,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/21257718/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/21257718/
</a>
</p>
页:
[1]