【问题标题】:Should it be possible to implement topLayoutGuide and bottomLayoutGuide in subclases of UIViewController?是否可以在 UIViewController 的子类中实现 topLayoutGuide 和 bottomLayoutGuide?
【发布时间】:2013-10-16 19:05:49
【问题描述】:

我一直在尝试创建一个容器视图控制器,它提供一些覆盖视图,如 UINavigationController 和 UITabBarController 为 iOS 7 中的视图控制器做的。为了使包含的视图布局正确,我尝试了几乎所有我能想到的关于在容器和包含的视图控制器中实现 -bottomLayoutGuide ,但没​​有运气。该方法被调用,但该值似乎没有被使用。

我在https://github.com/stefanfisk/custom-layout-guides 上整理了一个简单的示例,但我什至无法调用访问器。

【问题讨论】:

  • 我已经用一个简单项目的链接编辑了这个问题,该项目显示什么都没发生。

标签: uiviewcontroller ios7 autolayout


【解决方案1】:

我发现当你在代码中设置约束时,例如

[self.view addConstraints:[NSLayoutConstraint
                           constraintsWithVisualFormat:@"V:[topLayoutGuide][mainView]"
                           options:0
                           metrics:nil
                           views:@{@"topLayoutGuide" : self.topLayoutGuide, @"mainView" : self.mainView}]];

它崩溃了:

2013-10-16 22:23:27.119 Custom Layout Guides[46840:a0b] -[LayoutGuide superview]: unrecognized selector sent to instance 0x8c80c80
2013-10-16 22:23:27.124 Custom Layout Guides[46840:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[LayoutGuide superview]: unrecognized selector sent to instance 0x8c80c80'

Auto Layout 尝试在布局指南上调用 superview 很奇怪,因为它应该只符合 UILayoutSupport 协议。

我还注意到topLayoutGuidebottomLayoutGuide 被声明为readonly

@property(nonatomic, readonly, retain) id<UILayoutSupport> topLayoutGuide
@property(nonatomic, readonly, retain) id<UILayoutSupport> bottomLayoutGuide

【讨论】:

  • 我,似乎它们不应该被子类覆盖,但对我来说,文档看起来好像返回的实际对象将被布局引擎使用,这意味着覆盖它应该可以工作.框架实现返回的对象属于此类,这显然是一个视图:github.com/JaviSoto/iOS7-Runtime-Headers/blob/master/Frameworks/…
  • @StefanFisk:在我看来,在这种情况下,文档与实现不匹配。也许您可以找到一些不需要覆盖布局指南的解决方法?
  • 请注意:在内部,布局指南是符合 UILayoutSupport 添加到视图层次结构的 UIView。如果在将自定义 UIView 子类添加到视图后返回它们可能会起作用?
猜你喜欢
  • 1970-01-01
  • 2018-01-14
  • 2015-12-13
  • 2013-09-24
  • 2023-03-28
  • 2015-06-03
  • 1970-01-01
  • 2019-04-10
  • 2017-10-12
相关资源
最近更新 更多