【问题标题】:UINavBar override StatusBar and repeat background custom imageUINavBar 覆盖 StatusBar 并重复背景自定义图像
【发布时间】:2023-03-30 18:20:02
【问题描述】:

我正在将应用程序从 iOS 4.3 更新到 iOS 7,但我无法解决一个问题。我在网上做了很多搜索,但没有运气。有类似的问题,但对我不起作用:

link 1 link 2

我不使用故事板,我使用 xib 文件。 我在标签栏控制器中嵌入了导航控制器中嵌入的视图控制器。 我在 didFinishLaunchingWithOptions: 方法中添加我的观点:

    HomeViewController *homeViewController = [[HomeViewController alloc] init];
    homeViewController.tabBarItem.image = [UIImage imageNamed:@"home.png"];
    homeViewController.tabBarItem.title = NSLocalizedString(@"Home", @"Home");
    UINavigationController *homeNavController = [[UINavigationController alloc] initWithRootViewController:homeViewController];
    homeNavController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    //...other views
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:homeNavController, ..., nil];

要添加自定义背景图像,我使用代码(仍然使用相同的方法):

    [[UINavigationBar appearance] setTintColor:UIColorFromRGB(0xFFA722)];
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"Logo.png"] forBarMetrics:UIBarMetricsDefault];

我尝试添加 UINavigationBarDelegate 和:

- (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar
{
    return UIBarPositionTopAttached;
}

但是当我设置委托时,我尝试通过两种方式进行:

//在didFinishLaunchingWithOptions中

homeNavController.navigationBar.delegate = self;

在视图控制器中:

self.navigationController.navigationBar.delegate = self;

出现错误:

由于未捕获的异常而终止应用 'NSInternalInconsistencyException',原因:'不能手动设置 委托由控制器管理的 UINavigationBar。'

您知道如何解决此问题,使导航栏布局位于顶部而不覆盖状态栏吗?

另一个问题是我的应用中有尺寸为 320x44 和视网膜版本 640x88 的图像徽标,图像被重复。 为什么它的大小与导航栏一样? 提前致谢。

【问题讨论】:

  • @iDev in viewDidLoad 我添加了日志 NSLog(@"FRAME: %@", NSStringFromCGRect(self.navigationController.navigationBar.frame));它显示 44。我将应用程序从 iOS4 更新到 iOS7,但我仍然想支持 iOS 6(6 及更高版本)。
  • 您将导航栏大小设置为 320*64。因为导航栏与状态栏合并 44+20=64
  • 我可以将图像大小调整为 64,但仍会覆盖状态栏。

标签: ios objective-c uinavigationcontroller


【解决方案1】:

我使用的一种方式:

    UIView *navBar = nil;
    [_customBar removeFromSuperview];
    _customBar = nil;
    //
    CGFloat width = self.view.frame.size.width; // 320px
    CGFloat height = 110.0f;
    // subview height
    navBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    [navBar setBackgroundColor:[UIColor blueColor]];
    // redefine bar height
    UINavigationBar *currentNavBar = self.navigationController.navigationBar;
    CGRect frame = [currentNavBar frame];
    frame.size.height = height;
    frame.origin.x = 0.0f;
    [currentNavBar setFrame:frame];
    _normalBar = (UINavigationBar*) navBar;
    // ******************************
    // add your custom view to navBar
    // ******************************
    _normalBar = (UINavigationBar*) navBar;
    [self.navigationController.navigationBar addSubview:navBar];

别忘了恢复原来的吧:

[_normalBar removeFromSuperview];
_normalBar = nil;
CGRect frame =  self.navigationController.navigationBar.frame;
frame.size.height = 44.0f;
[self.navigationController.navigationBar setFrame:frame];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-03
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 2021-04-22
    • 1970-01-01
    相关资源
    最近更新 更多