uinavigationcontroller上的自定义标题浏览器正在错误地对
-
02-10-2019 - |
题
我可能在这里做错了什么,因为这看起来有点愚蠢。
我在我的UinavigationController上设置了一个自定义标题视图(以Uilabel的形式),每个页面都相同。为了促进这一点,我在应用程序委托中创建了一个函数,以正确显示标签。然后,将其推到导航堆栈后,我将在任何子视图上调用此功能。
这是代码(这可能比我的解释更有意义):
//In MyAppDelegate.m:
- (void)showTitleForNavigationController:(UINavigationController*) navController {
UILabel *label = [[UILabel alloc] init];
// set up label attributes
// ...
[label sizeToFit]; //without this line my label won't show at all
[navController.navigationBar.topItem setTitleView:label];
[label release];
}
// In SomeViewController.m, when pushing another controller onto the stack:
UIViewController *otherViewController = //initialize other view controller;
[self.navigationController pushViewController:otherViewController animated:YES];
[(MyAppDelegate*)[[UIApplication sharedApplication] delegate] showTitleForNavigationController:otherViewController.navigationController];
我的问题是,当我将下一个视图控制器推到堆栈上时,新的控制器在动画的整个过程中平稳地滑动,标签摇摆在左上方,然后在动画完成后最终捕捉到位置。看起来真的很奇怪。如何正确设置标签,以使下一个视图从下一个视图滑动?当然,我缺少这很简单...
解决方案 2
我最终要做的是将图像与文本一起用作标头的背面,因此,它根本没有动画,而不是像我最初想要的那样顺利进行动画。
考虑到它到处都是相同的趋势,但这并不是那么大。
其他提示
这个问题的一个很晚的答案,但是我只是遇到了同样的问题,并找到了另一种解决方法,而无需使用图像。以为我会分享我的解决方案,因为这可能会对某人有所帮助。
在我的情况下,我将自定义Uilabel设置为标题视图,我才意识到,只有当我在ViewDidload方法中设置TitleView属性时,它才能正确动画。但是,在某些情况下,我在ViewDidload中还不知道标题(在某些情况下,我需要使用HTTP请求中的标题)。因此,我对这些情况的解决方案是将TitleView属性设置为我的CustomLabel,并使用Text @“”在ViewDidload中,每当我获得真实标题时,我都只会更改自定义标签的文本属性。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
//set temporary title, the MBMUINavigationBarTitleView is a UIView subclass whose viewWithTitle method returns an autoreleased UIlabel with my custom settings, custom font etc.
self.navigationItem.titleView = [MBMUINavigationBarTitleView viewWithTitle:@" "];
}
//somewhere later, when I have the real title
UILabel* titleLabel = (UILabel*)self.navigationItem.titleView;
[titleLabel setText:theRealTitle];
我的情况与YLVA相似,使用自定义文本类的实例 UINavigationItem's
titleView
财产。但是,我发现将其配置为 viewDidLoad
没有解决动画故障。
我解决问题的方法是等到相关视图控制器被从导航控制器的堆栈中弹出,那时删除 UINavigationItem's
风俗 titleView
因此,它根本不需要动画。
当我的 UINavigationController
子类接收 popViewControllerAnimated:
消息,我从我的自定义文本字段中复制标题文本(UINavigationItem's
titleView
) 进入 UINavigationItem's
title
属性并设置 titleView
财产到零。然后 UINavigationController
继续前进并弹出视图控制器,只有标准的导航栏标签是动画的(不是我的自定义标题),glitch free。