简体   繁体   English

出现UINavigationController时调用什么方法?

[英]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设置为windowrootViewController时, 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.

相关问题 返回UINavigationController的根视图时会调用什么方法? - What methods are called when returning to UINavigationController's root view? 当UINavigationController通过Storyboard模态呈现时,它的rootViewController与它没有任何关系 - When UINavigationController is modally presented via Storyboards, it's rootViewController has no connection to it 当呈现为模态视图控制器时,ARC UINavigationController堆栈未被释放 - ARC UINavigationController stack not getting deallocated when presented as modal view controller 当 UIViewController 以模态呈现时,UINavigationController(在 UITabBarController 选项卡内)丢失堆栈 - UINavigationController (Inside a UITabBarController tab) losing stack when a UIViewController is presented modally 半透明的UINavigationController,以模态显示 - Translucent UINavigationController, presented modally 首次显示视图时,UIViewController中的哪个方法仅被调用一次? viewDidLoad? - Which method(s) in UIViewController is called only once, when the view is first presented? viewDidLoad? 快速地从Presented ViewController转到Previous ViewController时调用哪种方法 - Which method is called when we go from Presented ViewController to Previous ViewController in swift 在UINavigationController的子视图中调用旋转方法? - Rotation method called in child view of UINavigationController? 呈现的UINavigationController色彩不变 - Presented UINavigationController tint color not changing 向后导航时未调用UINavigationController上的ViewDidAppear - ViewDidAppear on a UINavigationController not getting called when navigating back
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM