我目前正在尝试重新工作this navigation dropdown menu,以便下拉菜单仅占用窗口的下半部分。尝试获取对容器视图控制器的引用
这是我的故事板设置:
不要担心外来标签/按钮。我只是想显示不同的视图控制器/连接与此图片。)
我有一个问题获取对容器视图控制器的引用,以便我可以定义框架。这就是我现在得到:
private var navigationController: UINavigationController?
,并声明该变量时:
self.navigationController = UIApplication.sharedApplication().keyWindow?.rootViewController?.presentedViewController?.presentedViewController?.childViewControllers[0] as? UINavigationController
println(self.navigationController!.nameOfClass) // logs: UINavigationController
self.navigationController = self.navigationController!.topMostViewController?.navigationController //
println(self.navigationController!.nameOfClass) // logs: Optional(<UINavigationController: 0x7f8681e7b5f0>)UINavigationController
这里就是我得到的错误 - 试图定义下拉菜单视图帧时:
var frame = CGRectMake(0, 0, titleSize.width + (self.configuration.arrowPadding + self.configuration.arrowImage.size.width)*2, self.navigationController!.navigationBar.frame.height)
我得到的错误:
fatal error: unexpectedly found nil while unwrapping an Optional value.
我知道这意味着什么,但我为什么价值为零而感到困惑。因为当我记录self.navigationController的类名时,我得到了正确的导航控制器。
而且,这里是为nameOfClass和topMostViewController参考:我试图要简洁,同时还提供足够的信息
extension UIViewController {
var topPresentedViewController: UIViewController? {
var target: UIViewController? = self
while (target?.presentedViewController != nil) {
target = target?.presentedViewController
}
print(target)
return target
}
var topVisibleViewController: UIViewController? {
if let navigation = self as? UINavigationController {
if let visibleViewController = navigation.visibleViewController {
return visibleViewController.topVisibleViewController
}
}
if let tab = self as? UITabBarController {
if let selectedViewController = tab.selectedViewController {
return selectedViewController.topVisibleViewController
}
}
return self
}
var topMostViewController: UIViewController? {
return self.topPresentedViewController?.topVisibleViewController
}
}
public extension NSObject{
public class var nameOfClass: String{
return NSStringFromClass(self).componentsSeparatedByString(".").last!
}
public var nameOfClass: String{
return NSStringFromClass(self.dynamicType).componentsSeparatedByString(".").last!
}
}
,但如果需要,我可以张贴整个项目。任何帮助将不胜感激:)
呃,只是想通了> _ < 当我指定框架的宽度,特别是arrowimage.size.width属性时,我得到了零。原来,我将对图像的引用复制到我的项目中,而不是实际的图像本身。不能相信我花了一整天的时间来坚持下去! 继续.. – jvolen2