[英]What method is called when UINavigationController is presented?
I am trying to subclass the UINavigationController
to add a custom subview underneath its UINavigationBar
. 我正在尝试对
UINavigationController
进行子类化,以在其UINavigationBar
下添加自定义子视图。 The problem is when I set my custom UINavigationController
as the rootViewController
of my window
, the UINavigationBar
position changes, so I want to update the position of my custom view accordingly. 问题是当我将自定义
UINavigationController
设置为window
的rootViewController
时, UINavigationBar
位置发生了变化,因此我想相应地更新自定义视图的位置。 My question is where should I put my code to update my custom view. 我的问题是我应该将代码放在哪里以更新自定义视图。
var navController = TabbedNavigationController(rootViewController: firstView)
// navController.navigationBar.frame.origin.y = 0
self.window?.rootViewController = navController
self.window?.makeKeyAndVisible()
// navController.navigationBar.frame.origin.y = 20.0
What you are trying to do is respond to the positioning of subviews of the navigation controller's view. 您要做的是响应导航控制器视图的子视图的位置。 Positioning of subviews is called layout , and takes place in
layoutSubviews
. 子视图的定位称为layout ,它发生在
layoutSubviews
。 Thus, you could try implementing viewDidLayoutSubviews
, which is called immediately after layoutSubviews
. 因此,您可以尝试实现
viewDidLayoutSubviews
,它在layoutSubviews
之后立即被调用。 Note that it can be called many times but you may want to add your subview only once, so be careful to add appropriate guards. 请注意,它可以多次调用,但是您可能只想添加一次子视图,因此请小心添加适当的防护。
You can force the "view" of the ViewController to extend underneath the UINavigationBar. 您可以强制将ViewController的“视图”扩展到UINavigationBar下方。 Here's some code to help you out, it's not Swift, it's ObjC but you will get the point.
这里有一些代码可以帮助您,它不是Swift,而是ObjC,但是您会明白这一点。 This is from my custom implementation of UIVeiwController subclass.
这来自我的UIVeiwController子类的自定义实现。 I also have a full implementation of UINavigationController and UINavigationBar to adjust the size in points of the UINavigationBar.
我也有UINavigationController和UINavigationBar的完整实现,可以调整UINavigationBar的磅数。 I can tell you that you can do what Apple has done in their example by just following the code I'm linking here.
我可以告诉您,只要遵循我在此处链接的代码,您就可以完成Apple在其示例中所做的事情。
[[[self navigationController] navigationBar] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[[[self navigationController] navigationBar] setShadowImage:[UIImage new]];
[[[self navigationController] navigationBar] setTranslucent:TRUE];
[[[[self navigationController] navigationBar] layer] setShadowOpacity:0.0f];
[[[[self navigationController] navigationBar] layer] setShadowOffset:CGSizeMake(0.0f, 0.0f)];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.