为所有navigationcontroller和navigationitem设置titleview

[[[UINavigationBar appearance] setTitleView:button] 

我尝试使用上面的代码行将所有导航项目的标题视图设置为button,但它不工作。如何通过在appdelegate中编写小代码来为我的项目中的所有导航栏设置标题视图?

自定义导航栏的外观

修改barStyle,tintColor和半透明属性是可以的,但是不能直接直接更改UIView级别的属性,如框架,边界,alpha或隐藏属性。

有关更多详细信息,请参阅 apple doc UINavigationBar Class Reference

编辑 – >

我解决了你的查询

 [[UINavigationBar appearance] addSubview:yourView]; 

其他导航相关的查询

试试这个家伙…

 //put whatever object in this view..for example button that you want to put... UIView* ctrl = [[UIView alloc] initWithFrame:navController.navigationBar.bounds]; ctrl.backgroundColor = [UIColor yellowColor]; ctrl.autoresizingMask = UIViewAutoresizingFlexibleWidth; [navController.navigationBar addSubview:ctrl]; 

让我知道它是否工作!

快乐编码!

这是如何在addDelegate中创build导航控制器并为其设置标题,您必须将以下代码添加到didFinishLaunchingWithOptions方法中。 请注意,您需要为viewController设置一个根视图,该视图将显示在navigationController内部的viewController中。

  yourViewController *mainVC = [[yourViewController alloc]init]; UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:mainVC]; navigationController1.title = @"title"; [window addSubview:navigationController1.view]; 

如果您使用iOS4,则可以覆盖drawRect

 @implementation UINavigationBar (UINavigationBarCategory) -(void)drawRect:(CGRect)rect { UIImageView *itleImageView = //create object [self addSubView:titleImageView]; //set title view in navigation bar } @end 

iOS 5,

 if ([[UIDevice currentDevice].systemVersion floatValue] >= 5.0) { if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){ UIImageView *itleImageView = //create object [self.navigationController.navigationBar addSubView:titleImageView]; } } 

我想这是一个正确的,因为我没有实施。

我想在每个NavigationController中添加一个徽标,所以我创build了UINavigationController一个子类,并在viewDidLoad使用了这个方法 – 方法

 UIImage *logo =[UIImage imageNamed:@"logo"]; CGFloat navWidth = self.navigationBar.frame.size.width; CGFloat navHeight = self.navigationBar.frame.size.height; UIImageView *logoView = [[UIImageView alloc] initWithFrame:CGRectMake(navWidth-logo.size.width - 5, (navHeight - logo.size.height) / 2, logo.size.width, logo.size.height)]; logoView.image = logo; [self.navigationBar addSubview:logoView];