无法将UIScrollView添加到UINavigationBar



我想将我的scrollview添加为UINavigationBar's titleview,但奇怪的是,我无法这样做。

在视图中DidLoad:

navController = [[UINavigationController alloc] init];
    self.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 20 - 45 - 2);
    CGRect frame = navController.view.frame;
    frame.size.height -= 20;
    [self.view addSubview:navController.view];
    UIScrollView *someview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    someview.backgroundColor = [UIColor orangeColor];
    navController.navigationItem.titleView = someview;

这是一段非常简单的代码,但我无法弄清楚我缺少了什么。请有人帮我…谢谢。

旁注:我在没有任何根控制器的情况下启动UINavigationController,因为我首先关心的是向导航栏添加滚动视图。

navController.navigationItem从未实际添加到导航栏中。它的存在是因为导航控制器是UIViewController的一个子类。但它没有被使用。

在导航控制器中添加根视图控制器,并添加滚动视图作为其navigationItem titleView

根据您想要的内容,您可能还想将导航控制器子类化,以便在推送任何新的视图控制器时注入您的自定义titleView

您是否尝试将滚动视图添加到容器视图中。类似这样的东西:

navController = [[UINavigationController alloc] init];
self.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 20 - 45 - 2);
CGRect frame = navController.view.frame;
frame.size.height -= 20;
[self.view addSubview:navController.view];
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(...)];
UIScrollView *someview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
someview.backgroundColor = [UIColor orangeColor];
[container addSubview:someview];
navController.navigationItem.titleView = container;

最新更新