UINavigationController 上的自定义 titleView 动画不正确

发布于 2024-09-19 01:43:10 字数 1084 浏览 10 评论 0原文

我可能在这里做错了什么,因为这看起来有点愚蠢。
我正在我的 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];

我的问题是,当我将下一个视图控制器推入堆栈时,新控制器平滑地滑动,在动画的整个持续时间内,标签粘在顶部在动画完成后最终卡入到位之前离开。它看起来真的很奇怪而且丑陋。 如何正确设置标签,使其顺利从下一个视图滑动?当然,这是我所缺少的简单的东西......

I'm probably doing something wrong here because this looks a bit stupid.
I'm setting up a custom titleView (in the form of a UILabel) on my UINavigationController that is the same on every page. To facilitate this, I've created a function in my app delegate to display the label correctly. I then call this function on any subviews just after I push it to the navigation stack.
Here's the code (which probably makes more sense than my explanation):

//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];

My problem is that when I push the next view controller onto the stack, and the new controller slides across smoothly, for the whole duration of the animation the label sticks to the top left before finally snapping into place after the animation is finished. It looks really odd and ugly.
How can I set up the label properly so that it slides from the next view smoothly? Surely it's something simple that I'm missing...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我只土不豪 2024-09-26 01:43:10

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

就我而言,我将自定义 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];

A very late answer to this question, but I just ran into the same problem and found another way to solve it, without using an image. Thought I'd share my solution as it might help someone.

In my case I'm setting a custom UILabel to be the titleview, and I realized that only when I set the titleview property in the viewDidLoad method, it animates correctly. In some cases however, i didn't knew the title yet in my viewDidLoad (in some cases i needed to use a title from a http request for example). So, my solution for those cases was to set the titleview property to my customlabel with text @" " in viewDidLoad, and whenever I got the real title I only changed the text property of my custom label.

- (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];
深居我梦 2024-09-26 01:43:10

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

What I ended up doing was using an image with the text included as the backround for the header, so rather than animating smoothly as I wanted originally, it's not animating at all.
Considering it's the same heading everywhere, it's not that big a deal though.

无尽的现实 2024-09-26 01:43:10

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

我解决这个问题的方法是等到有问题的视图控制器从导航控制器的堆栈中弹出,然后删除 UINavigationItem 的自定义 titleView,这样它就永远不需要变得生动起来。

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

I was in a similar situation to ylva, using an instance of a custom text class for the UINavigationItem's titleView property. However, I found configuring it in viewDidLoad didn't resolve the animation glitch.

My workaround to the problem was to wait until the view controller in question was being popped off the navigation controller's stack, and at that point remove the UINavigationItem's custom titleView so it never needs to get animated at all.

When my UINavigationController sub-class receives the popViewControllerAnimated: message, I copy the title text from my custom text field (UINavigationItem's titleView) into the UINavigationItem's title property and set the titleView property to nil. Then the UINavigationController goes ahead and pops off the view controller and only the standard navigation bar title label is animated (not my custom title), glitch free.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文