【发布时间】:2023-03-30 18:20:02
【问题描述】:
我正在将应用程序从 iOS 4.3 更新到 iOS 7,但我无法解决一个问题。我在网上做了很多搜索,但没有运气。有类似的问题,但对我不起作用:
我不使用故事板,我使用 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