【问题标题】:Issues with changing UINavigationBar background when presenting modal view controller呈现模式视图控制器时更改 UINavigationBar 背景的问题
【发布时间】:2012-03-20 04:12:43
【问题描述】:

我正在使用this 方法来更改我的uiviewcontrollers 的背景。它通常在我推送视图控制器时起作用。

但是,如果视图控制器是使用

[self presentModalViewController:customViewController animated:YES];

然后,此代码不起作用。任何人都可以提出什么问题吗?

使用的代码:

要在导航栏中显示图像,您必须自己绘制它,这实际上并不难。将其另存为 UINavigationBar+CustomBackground.m(它向 UINavigationBar 添加了一个自定义类别):

@implementation UINavigationBar (CustomBackground)

- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:@"NavMain.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

@end

【问题讨论】:

  • 你试过了吗:[self.navigationController presentModalViewController:customViewController animated:YES];

标签: objective-c ios uinavigationbar


【解决方案1】:

如果您在 iOS 5 上运行,则不再调用 drawRect:

您将需要使用UIAppearance 或子类UINavigationController 并使用它来更改您的图像。

UIAppearance的教程可以在here找到

drawRect: 仍适用于 iOS 5 以下的版本)

【讨论】:

  • 我的工作非常适合被推送的视图控制器。对于以模态方式呈现的那些,它会导致此问题。任何线索为什么?
  • 我不确定,但如果是 iOS 5 则不应该。除非setNeedsDisplay 被调用。虽然我不知道这是否有效。
  • 我不这么认为,但感谢您的努力。
【解决方案2】:

试试这个,

UIColor *image = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"imagename.png"]];

替换图片名称。

【讨论】:

    【解决方案3】:

    不确定是不是这样,但你试过了吗

    [self presentViewController:customViewController animated:YES completion:NULL]
    

    并设置您认为适合 customViewController 的任何 modalViewStyle?根据iOS documentation,presentModalViewController 已被弃用,因此使用上述消息调用可能会更好(尤其是因为您似乎只在模态视图控制器上遇到此问题)

    【讨论】:

      【解决方案4】:

      你必须创建一个导航控制器,设置根控制器并展示它。

      例如

      UIViewController *vc = [[UIViewController alloc] init];

      UINavigationController *cntrol = [[UINavigationController alloc] initWithRootViewController:vc];

      [self presentModalViewController:cntrol Animation:YES];

      【讨论】:

        【解决方案5】:

        在导航控制器中设置导航栏图像的方法。

        但是您提供的新视图控制器没有使用导航方法。相反,您使用模态视图控制器。

        我有两个解决方案:

        --- 1 -----

        仍然使用模态视图控制器,只需将该图像添加到新视图控制器的顶部即可。

        [self.view addSubview:theImage];
        

        ---- 2 -----

        使用

        [self.navigationController pushViewController:customViewController animated:YES];
        

        而不是

        [self presentModalViewController:customViewController animated:YES];   
        

        在这种情况下,您正在使用导航。

        我会推荐第二个。

        【讨论】:

          猜你喜欢
          • 2015-02-24
          • 2016-10-05
          • 1970-01-01
          • 1970-01-01
          • 2015-02-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多