【发布时间】:2015-06-11 11:37:33
【问题描述】:
我正在尝试从另一个 (ViewController) 加载一个导航控制器 (BasicSearchController)。在 Storyboard 中,我创建了 2 个导航控制器,它们不是由 segue 链接的,而是独立的导航控制器。每个都分别链接到 ViewController 和 BasicSearchController,并且每个视图控制器在导航栏中都有自己的标题文本。
在ViewController.m 我的应用程序中,我有一个导航栏项,它使用以下代码显示:
searchButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search-icon.png"] landscapeImagePhone:[UIImage imageNamed:@"search-icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(presentSearch)];
当按钮被按下时,这个方法被调用:
- (void) presentSearch {
NSLog(@"Search presented!");
[self performSelector:@selector(pushBasicSearchController) withObject:self afterDelay:0.0];
}
最后,选择器运行这段代码:
- (IBAction)pushBasicSearchController {
BasicSearchController *basicController = [[BasicSearchController alloc] init];
[self presentViewController:basicController animated:YES completion:NULL];
}
在随后出现的 BasicSearchController 中,我有以下代码来生成用户界面:
- (void) createInterface {
screenRect = [[UIScreen mainScreen] bounds];
screenWidth = screenRect.size.width;
screenHeight = screenRect.size.height;
UIImage *navBackgroundImage = [UIImage imageNamed:@"bar.png"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background1.png"]];
[backgroundView setFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
[backgroundView setContentMode:UIViewContentModeScaleToFill];
[[self view] addSubview:backgroundView];
[[self view] sendSubviewToBack:backgroundView];
underBarGradient = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gradient-under-bar.png"]];
[underBarGradient setFrame:CGRectMake(0, 64, screenWidth, 3)];
[underBarGradient setContentMode:UIViewContentModeScaleToFill];
[underBarGradient setAlpha:0.5f];
[[self view] addSubview:underBarGradient];
[[self view] bringSubviewToFront:underBarGradient];
menuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu.png"] landscapeImagePhone:[UIImage imageNamed:@"menu.png"] style:UIBarButtonItemStyleDone target:self action:@selector(presentMenu)];
navigationItemMain.leftBarButtonItem = menuButton;
searchButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search-icon.png"] landscapeImagePhone:[UIImage imageNamed:@"search-icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(presentSearch)];
navigationItemMain.rightBarButtonItem = searchButton;
NSLog(@"UI created!");
}
underBarGradient 和 backGroundView 正确显示。但是, NagivationBar 及其标题文本不可见,并且导航栏项目也丢失了。
回到ViewController,我在AppDelegate.m中使用了如下代码来自定义导航栏:
I have modified `ViewController`'s navigation bar's appearance using the following code, which is found in `AppDelegate.m`:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"bar.png"] forBarMetrics:UIBarMetricsDefault];
NSShadow* shadow = [NSShadow new];
shadow.shadowOffset = CGSizeMake(1.0f, 1.0f);
shadow.shadowColor = [UIColor clearColor];
[[UINavigationBar appearance] setTitleTextAttributes: @{
NSForegroundColorAttributeName: [UIColor whiteColor],
NSFontAttributeName: [UIFont fontWithName:@"Aliquam" size:20.0f],
NSShadowAttributeName: shadow
}];
return YES;
}
然而,虽然这适用于 ViewController 的导航栏,但 BasicSearchController 的导航栏并没有被这段代码修改。
我的查询是:
- 如何使用 AppDelegate 中使用的代码自定义
BasicSearchController的导航栏? - 如何让导航栏项目出现在
BasicSearchController中?
【问题讨论】:
标签: ios objective-c uinavigationcontroller uinavigationbar viewcontroller