我正在努力实现的目标。 每当我有一个继承 self 的类 SuperViewController 的 UIViewController 时,在我的 super View Controller 的 viewDidLoad 中,我获取我的 UIViewController 的所有 subview ,创建一个 UIScrollView,将所有 subview 添加到它并将此 UIScrollView 设置为 UIViewController 的唯一 subview 。
调用[super viewDidLoad]前的UIViewController结构;
UIViewController
UIView
subview1
subview2
subview3
之后
UIViewController
UIView
UIScrollView
subview1
subview2
subview3
关于 UIView - subview 的约束,移动到 UIScrollView-subviews
我这样做的原因是因为在我们的应用程序中我们有很多处理 scrollViews 的事情,我只想编写代码将它们添加到父类(super class)中并让它根据内容启用/禁用滚动大小。
我目前在我的 superviewController 中有什么(简化)
- (void)viewDidLoad
{
[super viewDidLoad];
if([self class]!=[SuperViewController class] && [self isKindOfClass:[SuperViewController class]])
{
//subclass called this method
UIScrollView *scrollViewToAdd = [[UIScrollView alloc] init];
UIView *originalView = self.view;
NSArray* savedConstraints = self.view.constraints;
for (UIView* view in self.view.subviews)
{
[scrollViewToAdd addSubview:view];
}
for (NSLayoutConstraint*oldConstr in savedConstraints)
{
//loop through all of the constraints associated with the UIView. If one of the items is UIView, replace it with UIScrollView
//add all of these constraints to UIScrollView
NSLayoutConstraint *newConstraint = [NSLayoutConstraint constraintWithItem:oldConstr.firstItem==self.view?scrollViewToAdd:oldConstr.firstItem attribute:oldConstr.firstAttribute relatedBy:oldConstr.relation toItem:oldConstr.secondItem==self.view?scrollViewToAdd:oldConstr.secondItem attribute:oldConstr.secondAttribute multiplier:oldConstr.multiplier constant:oldConstr.constant];
newConstraint.priority = oldConstr.priority;
[scrollViewToAdd addConstraint:newConstraint];
}
}
//moving the subviews sets self.view to nil, thankfully i kept a reference so i can set it to the original value
_scrollView = scrollViewToAdd;
self.view = originalView;
//add scrollview as a child
[self.view addSubview:_scrollView];
//add scrollView constraints
id topGuide =self.topLayoutGuide;
id bottomGuide = self.bottomLayoutGuide;
_scrollViewToTop = [[NSLayoutConstraint constraintsWithVisualFormat: @"V: [topGuide]-0-[_scrollView]"
options: 0
metrics: nil
views: NSDictionaryOfVariableBindings (topGuide, _scrollView )] firstObject];
_scrollViewToBottom = [[NSLayoutConstraint constraintsWithVisualFormat: @"V:[_scrollView]-0-[bottomGuide]"
options: 0
metrics: nil
views: NSDictionaryOfVariableBindings (bottomGuide, _scrollView)] firstObject];
[self.view addConstraint:_scrollViewToTop];
[self.view addConstraint:_scrollViewToBottom];
}
这给了我不明白的约束冲突
(
"<_UILayoutSupportConstraint:0xb866db0 V:[_UILayoutGuide:0xf9b2750(0)]>",
"<_UILayoutSupportConstraint:0xb866cf0 V:|-(0)-[_UILayoutGuide:0xf9b2750] (Names: '|':UIView:0x9e90480 )>",
"<_UILayoutSupportConstraint:0xb866f90 V:[_UILayoutGuide:0xb862070(0)]>",
"<_UILayoutSupportConstraint:0xb866ec0 _UILayoutGuide:0xb862070.bottom == UIView:0x9e90480.bottom>",
"<NSLayoutConstraint:0xb87a710 V:[_UILayoutGuide:0xf9b2750]-(0)-[UIScrollView:0x9e84b00]>",
"<NSLayoutConstraint:0xb861e80 V:[UIScrollView:0x9e84b00]-(0)-[_UILayoutGuide:0xb862070]>",
"<NSAutoresizingMaskLayoutConstraint:0xf9a5280 h=--& v=--& UIScrollView:0x9e84b00.midY ==>",
"<NSAutoresizingMaskLayoutConstraint:0xf992950 h=-&- v=-&- UIView:0x9e90480.height == UIViewControllerWrapperView:0xb87bd20.height - 64>",
"<NSAutoresizingMaskLayoutConstraint:0xb870f90 h=-&- v=-&- UIViewControllerWrapperView:0xb87bd20.height == UINavigationTransitionView:0x9a6e3e0.height>",
"<NSAutoresizingMaskLayoutConstraint:0xf9b3560 h=-&- v=-&- UINavigationTransitionView:0x9a6e3e0.height == UILayoutContainerView:0x9a6dd10.height>",
"<NSLayoutConstraint:0xb876390 'UIView-Encapsulated-Layout-Height' V:[UILayoutContainerView:0x9a6dd10(480)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0xb861e80 V:[UIScrollView:0x9e84b00]-(0)-[_UILayoutGuide:0xb862070]>
如果不是
,我可以消除这个冲突 self.view = originalView;
[self.view addSubview:_scrollView];
我愿意
[self.view = _scrollView];
但这不会产生我想要的 View 层次结构。
最佳答案
如果您需要像 ViewController 的 Root View 一样加载 ScrollView,您需要重写 loadView 方法,而无需像这样调用 super:
-(void)loadView { _scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero]; [自行设置 View :self.scrollView];
约束不适用于 AutoresizeMask。如果您向 View 添加约束,则需要设置此 View [view setAutoresizingMask:NO]; 否则您会遇到冲突。
在设置约束之前添加 subview 。
关于iOs - 将 uiview 插入代码中的另一个 View ,保持约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23958238/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has
我需要从一个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=>
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer