UINavigationController 和 titleView
我的应用程序适合 UINavigationController 。 只需将 MyUIViewController
设置为 navigationController
的 rootViewController
,然后为 设置一个
。customTitleView
viewDidLoad
内myViewController
的navigationItem
现在,当我推送 newViewController
时,我希望看到之前的 customTitleView
(如 Apple NavigationController 参考所述),但事实并非如此。
出了什么问题?
下面的一小部分代码和 Apple UINavigationController 参考
“如果新的顶级视图控制器具有自定义标题视图,导航栏会显示该视图而不是默认标题视图。要指定自定义标题视图,请设置视图控制器导航项的 titleView 属性。”
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationItem setTitleView:customTitleView];
}
也许“默认标题视图”意味着“什么都没有”?我把它解释为之前的titleView。
编辑
我正在尝试解决方法,但我还有其他一些问题。 解决方法是:
在我推入NavigationController的每个ViewController中,我在viewDidLoad中设置titleView,从rootViewController获取它,
UiView *originalContainer = [[[self.navigationController.viewControllers objectAtIndex:0] navigationItem] titleView]
我创建一个newTitleView并将其放入originalContainer.subviews(UIButton)中,因为我需要从中执行Target操作。
对于第一个推送的控制器来说,所有操作都完美,但是如果我在堆栈中推送另一个 viewController(第二个推送的),我就会丢失对 navigationController 的所有引用。每个实例变量都是nil。
该值是在 viewDidLoad
firstViewControllerPushed.navigationController = (UINavigationController*)0x6693da0 内获取的 FirstViewControllerPushed.parentViewController = (UINavigationController*)0x6693da0
SecondViewControllerPushedFromFirstViewControllerPushed.navigationController = 0x0 SecondViewControllerPushedFromFirstViewControllerPushed.parentViewController = 0x0
看来第二个ViewControllerPushed无处可去!
怎么可能?
我仔细检查了我是否正确推送了 viewController 而不是以模态方式呈现它,因为
这个问题我无法为第二个ViewControllerPushed 正确设置 newTitleView 。
My application fit inside a UINavigationController
.
Simply I setted up a MyUIViewController
as rootViewController
of navigationController
and than I set up a customTitleView
for the navigationItem
of myViewController
inside viewDidLoad
.
Now when I push a newViewController
I expect to see the previous customTitleView
(as described by Apple NavigationController reference) but it doesn't.
What's wrong?
A little part of code below and the Apple UINavigationController Reference
"If the new top-level view controller has a custom title view, the navigation bar displays that view in place of the default title view. To specify a custom title view, set the titleView property of the view controller’s navigation item."
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationItem setTitleView:customTitleView];
}
Maybe the "default title view" means "nothing"? I interpreted it as the previous titleView.
EDIT
I'm trying a work around but I have some other issues.
The work around is:
In every ViewController that I push inside the NavigationController I setup in viewDidLoad the titleView getting it from the rootViewController
UiView *originalContainer = [[[self.navigationController.viewControllers objectAtIndex:0] navigationItem] titleView]
I create a newTitleView and I put inside the originalContainer.subviews (UIButton) cause I need the Target action from it.
All works perfectly for the first pushed controller but if I push another viewController in the stack (the second one pushed) I loose every reference to the navigationController. Every instance variables are nil.
This values are getted inside the viewDidLoad
firstViewControllerPushed.navigationController = (UINavigationController*)0x6693da0
firstViewControllerPushed.parentViewController = (UINavigationController*)0x6693da0
secondViewControllerPushedFromFirstViewControllerPushed.navigationController = 0x0
secondViewControllerPushedFromFirstViewControllerPushed.parentViewController = 0x0
It seems that the secondViewControllerPushed lives nowhere!!
How it's possible?
I double checked that I correctly push the viewController instead of present it modally
Couse this issue I'm not able to make a right setup of the newTitleView for the secondViewControllerPushed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个一半的黑客,但我觉得它给了你在你正在使用的 viewController 中更多的控制权。所以下面是我设置的一个小方法,然后我只是在 viewDidLoad 中调用它 - [self setUpNavBar];
使用 [UIButton buttonWithType:101] 完全没问题,因为它通过了 Apple 验证。
This is a hack in a half but I feel it gives you a bit more control in the viewController you are working in. So below is a small method I have setup and then I just call it in viewDidLoad - [self setUpNavBar];
And using [UIButton buttonWithType:101] is perfectly fine as it passed Apple Validation.