【问题标题】:Custom titleView on UINavigationController is animating incorrectlyUINavigationController 上的自定义 titleView 动画不正确
【发布时间】:2010-09-13 00:27:49
【问题描述】:

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

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

【问题讨论】:

    标签: cocoa-touch animation uinavigationcontroller titleview


    【解决方案1】:

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

    在我的例子中,我将自定义 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];
    

    【讨论】:

    • titleLabel 会自行调整大小吗?
    • 如果我没记错的话,我有一个函数可以在文本更改时调整标签大小。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多