8

我可能在这里做错了什么,因为这看起来有点愚蠢。
我在我的 UINavigationController 上设置了一个自定义 titleView(以 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];

我的问题是,当我将下一个视图控制器推入堆栈时,新控制器平滑地滑过,在动画的整个持续时间内,标签都会粘在左上角,然后在动画完成后最终卡入到位。它看起来真的很奇怪和丑陋。如何正确设置标签,使其从下一个视图顺利滑动?当然,我错过了一些简单的事情......

4

3 回答 3

3

这个问题的答案很晚,但我刚刚遇到了同样的问题,并找到了另一种解决方法,而不使用图像。以为我会分享我的解决方案,因为它可能会对某人有所帮助。

在我的情况下,我将自定义 UILabel 设置为 titleview,并且我意识到只有在 viewDidLoad 方法中设置 titleview 属性时,它才能正确设置动画。然而,在某些情况下,我还不知道 viewDidLoad 中的标题(例如,在某些情况下,我需要使用来自 http 请求的标题)。因此,我针对这些情况的解决方案是在 viewDidLoad 中将 titleview 属性设置为带有文本 @" " 的自定义标签,并且每当我获得真正的标题时,我只更改自定义标签的文本属性。

- (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];
于 2011-08-29T16:58:53.590 回答
0

我最终做的是使用包含文本的图像作为标题的背景,所以不是像我最初想要的那样流畅地制作动画,它根本不是动画。
考虑到到处都是相同的标题,但这没什么大不了的。

于 2011-03-21T01:14:30.317 回答
0

我的情况与 ylva 类似,使用该UINavigationItem's titleView属性的自定义文本类的实例。但是,我发现配置它viewDidLoad并没有解决动画故障。

我对这个问题的解决方法是等到有问题的视图控制器从导航控制器的堆栈中弹出,然后删除UINavigationItem's自定义titleView,所以它根本不需要动画。

当我的UINavigationController子类收到popViewControllerAnimated:消息时,我将自定义文本字段 ( UINavigationItem's titleView) 中的标题文本复制到UINavigationItem's title属性中并将属性设置titleView为 nil。然后UINavigationController继续并弹出视图控制器,只有标准导航栏标题标签是动画的(不是我的自定义标题),没有故障。

于 2015-02-23T04:54:16.270 回答