【问题标题】:Strange behavior of titleView that does not appear until back button is pressedtitleView 的奇怪行为直到按下后退按钮才出现
【发布时间】:2012-01-15 10:27:28
【问题描述】:

viewDidLoad 中,我有一个 UINavigationBar,其自定义背景通过以下方式实现:

[self.navigationController.navigationBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:nav_bg_image]] atIndex:100];

在栈顶(称为RVC)我有以下完整代码:

UIImageView *logoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:navbar_image_logo]];
[self.navigationController.navigationBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:nav_bg_image]] atIndex:100];
[self.navigationItem setTitleView:logoView];
[logoView release];

这非常有效。但是,当我在堆栈上推送一个新的VC 并在viewDidLoad 中使用相同的代码时

UIImageView *logoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:navbar_image_logo]];
[self.navigationController.navigationBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:nav_bg_image]] atIndex:1];
[self.navigationItem setTitleView:logoView];
[logoView release];
UIBarButtonItem *hButton = [[UIBarButtonItem alloc] initWithTitle:@"Filters" style:UIBarButtonItemStylePlain target:self action:@selector(filtersAction)];
self.navigationItem.rightBarButtonItem = hButton;
[hButton release];

“过滤器”按钮和titleView imageView都没有出现。但是,我知道过滤器按钮已添加到 UI,因为如果我单击它应该出现的区域,则会执行操作。

此外,当我深入到详细视图DVC 并返回时,突然出现了 titleView (logoView)!

我怀疑这是某种内存问题,但我无法解决。

【问题讨论】:

    标签: iphone objective-c memory-management uinavigationcontroller uinavigationbar


    【解决方案1】:

    您对insertSubView:atIndex: 方法的使用看起来很可疑。你在那个视图中真的有 100 个子视图吗?

    【讨论】:

    • 不,最初我在 index:0 处拥有它,但我想(出于调试目的)确保它不会取代另一个视图,因为它占据了整个视图。 - 它不适用于 index:0、1 或 100。
    • 只能在有效索引处插入。如果您希望添加的子视图出现在“前面”,只需使用-addSubview:(即索引参数与zIndex相同)
    【解决方案2】:

    提到了您在 iDev Recipes How do iPhone apps Instagram/Reeder/DailyBooth implement custom NavigationBars with variable width back buttons?订购时遇到的问题

    基本上你以错误的方式添加背景。

    仅支持 iOS 5
    实现类似setBackgroundImage:forBarMetrics:的方法

    支持旧版 iOS + iOS 5

    1. 继承UINavigationBar并实现drawRect

      //.h
      @interface CustomNavigationBar : UINavigationBar
      @end
      
      //.m
      @implementation CustomNavigationBar
      
      - (void)drawRect:(CGRect)rect;
      {
          UIImage *image = [[UIImage imageNamed: @"BackgroundImage"] stretchableImageWithLeftCapWidth:0 topCapHeight:0];
          [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
      }
      
      @end
      
    2. 要让UINavigationController 使用您的子类,您需要使用 xib 创建它。


    如果您已经在代码中设置了所有内容,那么您仍然需要使用 nib 来执行此操作。 我使用的笔尖没有File's Owner,只包含一个Navigation Controller,它被配置为使用我的CustomNavigationBar,并且没有viewControllers

    在这种情况下,我在这样的代码中进行了设置:

    // Load my nib with only a Navigation Controller and custom navigationBar
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomNavigationController" owner:nil options:nil];
    
    // The only top level object will be my navigationController
    UINavigationController *navigationController = [topLevelObjects objectAtIndex:0];
    
    // Push a view controller to be the rootViewController
    [navigationController pushViewController:rootViewController animated:NO];
    

    然后展示这个控制器,但我通常会这样做。呈现它的控制器/窗口将在UINavigationController 上进行所需的保留

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-17
      • 2021-05-12
      • 2019-03-27
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 2013-04-03
      • 2015-11-19
      相关资源
      最近更新 更多