希望你也可以帮助这个... 我想通过添加自定义背景图片来自定义我的导航栏。我发现了完美但有点复杂的方法叫做:NavigationSwizzle。在以前的项目中,我没有遇到任何严重的问题,但现在我被卡住了......
从我的AppDelegate我调用这个静态方法:
@implementation SCAppUtils
+ (void)customizeNavigationController:(UINavigationController *)navController
{
//Customizing navigation BAR
UINavigationBar *navBar = [navController navigationBar];
[navBar setTintColor:kSCNavBarColor];
//Customizing TOOLBAR
[navController setToolbarHidden:NO animated:YES];
UIToolbar *toolBar = [navController toolbar];
UIImageView *imageView = (UIImageView *)[navBar viewWithTag:kSCNavBarImageTag];
UIImageView *imageView2 = (UIImageView *)[toolBar viewWithTag:kSCNavBarImageTag];
if (imageView == nil)
{
Utilities *utilities = [[Utilities alloc]init];
UIImage *img = [UIImage imageNamed:@"nav-bar-background.png"];
UIImage *img2 = [UIImage imageNamed:@"toolbar-background.png"];
imageView = [[UIImageView alloc] initWithImage:img];
imageView2 = [[UIImageView alloc] initWithImage:img2];
[imageView setTag:kSCNavBarImageTag];
[imageView2 setTag:kSCNavBarImageTag];
[navBar insertSubview:imageView atIndex:0];
[toolBar insertSubview:imageView2 atIndex:0];
[imageView release];
[imageView2 release];
[utilities release];
}
}
在rootNavigationController中,通过在堆栈上按下它来调用第二个视图控制器,我的右键,self.title,activityIndicator不显示。如果我在行 [navBar insertSubview:imageView atIndex:0]中注释或设置 atIndex:-1 ; 然后按钮显示,但我的自定义背景消失了,我得到了常规的iPhone导航标签。
在 rootNavigationController 的 didSelectRowAtIndex 中,我有:
//ADD BACK BUTTON TO VIEW
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Home" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
//Home Button color
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:166.0 / 255 green:179.0 / 255 blue:191.0 / 255 alpha:1.0];
//PUSH TO NEXT VIEW
[self.navigationController pushViewController:detailViewController animated:YES];
提前谢谢你......
如果您需要更多代码或解释,请不要犹豫,问我......
祝你好运, 卢卡
修改
好的,我可以让这个问题更加简单。我的导航栏中有自定义背景图像。这个图像显然是在覆盖我的
self.navigationItem.title = @"TITLE";
如果我把alpha放到:背景中的imageView的0.3,我可以看到我的标题:
imageView = [[UIImageView alloc] initWithImage:image];
[imageView setAlpha:0.3];
[imageView setTag:kSCNavBarImageTag];
[navBar insertSubview:imageView atIndex:0];
所以问题是如何将navigationItem.title发送到前面?它是UIView类的后代吗?
编辑2: 真的很奇怪的是,在rootController中标题是可见的(在背景图像上方),但是在推送另一个viewController标题之后会出现在背景imageView后面吗?!
答案 0 :(得分:1)
老实说,我觉得你忘了在main.m中添加这段代码。你main.m应该是这样的:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "SCClassUtils.h"
int main(int argc, char *argv[])
{
[SCClassUtils swizzleSelector:@selector(insertSubview:atIndex:)
ofClass:[UINavigationBar class]
withSelector:@selector(scInsertSubview:atIndex:)];
[SCClassUtils swizzleSelector:@selector(sendSubviewToBack:)
ofClass:[UINavigationBar class]
withSelector:@selector(scSendSubviewToBack:)];
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
答案 1 :(得分:0)
您可能希望尝试此操作来自定义导航栏。我在我的应用程序中使用它,它就像一个魅力
UIImage *theImage = [UIImage imageNamed:@"barImage.png"];
[self.navigationController.navigationBar insertSubview:[[[UIImageView alloc] initWithImage:theImage] autorelease] atIndex:0];
看看是否可以解决您没有出现按钮的问题。
答案 2 :(得分:0)
我为自己的工作找到了一份宣言。创建一个视图(viewBackground),其中包含符合导航栏的所有图像和颜色,然后将其转换为图像并将其用作背景
UIGraphicsBeginImageContextWithOptions(viewBackground.bounds.size, viewBackground.opaque, 0.0);
[viewBackground.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[UINavigationBar appearance] setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];