- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个问题,UIViewController
(来自 XIB)中的 topLayoutGuide.length
在 viewWillAppear
之后设置,我不知道如何 Hook topLayoutGuide.length
的更改以初始设置 TableView 的 contentOffset。
在 UINavigationController
中模态呈现 UIViewController
的代码:
let viewController = UIViewController(nibName: "ViewController", bundle: nil)
let navigationController = UINavigationController(rootViewController: viewController)
present(navigationController, animated: true, completion: nil)
关于 topLayoutGuide.length
我的调试输出
Init view controller
-[UIViewController topLayoutGuide]: guide not available before the view controller's view is loaded
willMove toParentViewController - top layout guide nan
Init navigation controller and pass view controller as root vc
Present navigation controller modally
viewDidLoad - top layout guide 0.0
viewWillAppear - top layout guide 0.0
viewWillLayoutSubviews - top layout guide 64.0
viewDidLayoutSubviews - top layout guide 64.0
viewWillLayoutSubviews - top layout guide 64.0
viewDidLayoutSubviews - top layout guide 64.0
viewDidAppear - top layout guide 64.0
didMove toParentViewController - top layout guide 64.0
viewWillLayoutSubviews - top layout guide 64.0
viewDidLayoutSubviews - top layout guide 64.0
现在我在 View Controller 中使用 bool 标志来设置 viewDidLayoutSubviews
中的 contentoffset 仅一次,即使该方法被调用多次。
有没有更优雅的解决方案?
最佳答案
documentation for the topLayoutGuide
明确指出:
Query this property within your implementation of the
viewDidLayoutSubviews()
method.
根据自己的观察,最早获取topLayoutGuide
实际长度的点是在viewWillLayoutSubviews()
方法中。但是,我不会依赖它,而是按照文档的建议在 viewDidLayoutSubviews()
中进行操作。
... 布局指南是依赖于任何容器 View Controller 布局的对象。当需要在屏幕上显示时,这些 View 会延迟布局。因此,当您将 viewController
添加到 navigationViewController
作为其 Root View Controller 时,它还没有布局。
布局发生在您呈现 navigationController
时。那时,两个 View Controller 的 View 都已加载(→ viewDidLoad()
、viewWillAppear()
),然后触发布局传递。首先,navigationViewController
的 View 被布局(布局流程:superview → subview)。导航栏的框架设置为 64 像素的高度。现在可以设置viewController
的topLayoutGuide
了。最后 viewController
的 View 被布局(→ viewWillLayoutSubviews()
, viewDidLayoutSubviews()
)。
根据布局指南的长度进行一些初始布局调整的唯一方法是您自己建议的方法:
在您的 View Controller 中有一个您最初设置为 true
的 bool 属性:
var isInitialLayoutPass: Bool = true
在 viewDidLayoutSubviews()
中检查该属性并仅在它为 true
时执行初始布局:
func viewDidLayoutSubviews() {
if isInitialLayoutPass {
tableView.contentOffset = CGPoint(x: 0, y: topLayoutGuide.length)
}
}
在 viewDidAppear()
中,将属性设置为 false
以指示初始布局已完成:
override func viewDidAppear() {
super.viewDidAppear()
isInitialLayoutPass = false
}
我知道它仍然感觉有点老套,但恐怕这是(我能想到的)唯一的方法,除非你想使用key-value-observing (KVO)在我看来并没有使它变得更整洁。
关于ios - topLayoutGuide 在 viewWillAppear 之后应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43963559/
我有一个支持 iPhone 上每个方向的 viewController (mainView)。 从这个 View 中,另一个 viewController 以模态样式呈现,它仅支持纵向方向(并相应地强
由于我试图“正确地”实现 iAds(即共享 ADBannerView 的单个实例),我在 UIViewController 中以编程方式创建了一个 UITableView 并将其添加到 View 中。
我正在编写一个自定义 UIViewController,它将具有类似标签栏的功能(包含用于在 subview 之间切换的控件)。添加子 Controller 时,我还将 subview 添加到容器 V
我有一个 View ,我希望它看起来像这样: |---------------| | | ", "" ) 看来我需要一些帮助。如何正确地做到这一点?我不想在 UIV
我在使用 topLayoutGuide 方法时遇到了奇怪的问题,我必须在 setAutomaticallyAdjustsScrollViewInsets: 不起作用的情况下使用它。为了缩小问题的原因,
我有一个用于呈现 View Controller 的自定义转换,并使用 UIPresentationController 子类来执行呈现。 在演示 Controller 中,我向其 container
我有一个问题,UIViewController(来自 XIB)中的 topLayoutGuide.length 在 viewWillAppear 之后设置,我不知道如何 Hook topLayoutG
我有一个简单的 View Controller 层次结构,其中一个 View Controller 使用 UINavigationController 显示。在 viewDidLoad 中,它设置了带
我有一个带有半透明状态栏和导航栏的 UIPageViewController。正如预期的那样,它的 topLayoutGuide 是 64 像素。 但是,UIPageViewController 的
如果有人能展示如何使用 iOS 7 中引入的 UITableViewController 的 topLayoutGuide 和 bottomLayoutGuide 属性,我将不胜感激。谢谢。 最佳答案
我正在使用 Swift2.1,我想在我的 View Controller View 中添加一个 subview ,它在屏幕顶部有一个导航栏。所以我尝试设置subview.top等于self.topLa
我正在学习通过代码在 VFL 中编写约束 我的 View 水平居中: let constraintY = NSLayoutConstraint.constraintsWithVisualFormat(
Apple 的 documentation关于在 View 和布局指南之一之间创建自动布局约束仅显示使用 VFL 的示例. 有没有办法不VFL(使用NSLayoutConstraint's other
我一直在尝试创建一个容器 View Controller ,它提供一些覆盖 View ,如 UINavigationController 和 UITabBarController 为 iOS 7 中的
我的 ViewController 嵌入在 UINavigationController 中,它有一个 view 应该与 Root View 的顶部对齐。 Root View 具有深灰色背景,并占据全
例如,我制作了一个类似于 UINavigationController 的自定义容器 View Controller ,但它的顶部有 2 个导航栏。 然后,在 subview Controller 中
我正在实现一个自定义容器,它与 UINavigationController 非常相似,只是它不包含整个 Controller 堆栈。它有一个 UINavigationBar,它被限制在容器 Cont
UIViewController 的 topLayoutGuide 和 bottomLayoutGuide 在 iOS 11 中已弃用。应该替换什么? 最佳答案 以前在您的 UIViewControl
我有一个 UIViewController 显示另一个 UIViewController。两个 View Controller 都使用带有自动布局的 topLayoutGuide 和 bottomLa
使用今天下载的 XCode 5。 iOS 7。当我运行应用程序时,表格 View 标题出现在导航栏下方,我不介意它在导航栏下方滚动,但我希望它最初看起来畅通无阻。我想弄清楚如何使用 iOS 7 转换指
我是一名优秀的程序员,十分优秀!