我一直在上下搜索以尝试解决这个问题,希望有人能提供帮助(我认为这可能是微不足道的,但我只是把情况弄糊涂了!)
我正在使用 XCode 5 和 Storyboard。我有一个容器 View ,它占据了 iPhone 上大约 75% 的屏幕空间。在此之上我有一些按钮(和其他小部件)。我希望能够在按下其中一个按钮时将 View Controller 加载到容器中。
到目前为止,所有这些工作正常,但正在加载的 View Controller 大小不同,并且将来可能会发生变化。我希望能够允许 subview (任意大小)滚动。
我试过将容器嵌入 ScrollView 中,但是作为添加新 subview 过程的一部分,您必须设置框架大小,但我希望 child 弄清楚它应该有多大然后相应地设置所有内容。
我不完全确定解决此问题的最佳方法是什么(在 Storyboard 中设置约束,还是以编程方式?)
任何建议都会很棒!或者指向教程的链接将非常有用,我唯一能找到的是将 View Controller 添加到容器 View 或将 View 添加到 ScrollView ,这些似乎都不能像 Xcode 5 (iOS 7) 预期的那样工作
谢谢!
-编辑-
这是我用来呈现任意大小的详细 View Controller 的代码,其余的都在 Storyboard 中设置(以防万一这对将来的任何人都有用!)现在可以工作了:
- (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];
}
最佳答案
您可以在任一位置设置约束并使其正常工作。秘诀在于:您必须将容器 View 的约束设置为包含它的 ScrollView 的顶部、左侧、底部和右侧。然后,Autolayout 将根据容器 View 内部的内容正确计算 ScrollView 的内容大小。
请参阅标题为 Pure Auto Layout Approach 的部分在 iOS 6 发行说明中。甚至还有一些示例代码。
// 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]];
关于ios - 将容器 View 嵌入 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21257718/
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
除了可访问性标准不鼓励使用这一事实指向当前页面的链接,我应该怎么做重构以下View代码?#navigation%ul.tabbed-ifcurrent_page?(new_profile_path)%li{:class=>"current_page_item"}=link_tot("new_profile"),new_profile_path-else%li=link_tot("new_profile"),new_profile_path-ifcurrent_page?(profiles_path)%li{:class=>"current_page_item"}=link_tot("p
print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上