2
[[[UINavigationBar appearance] setTitleView:button]

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

4

5 回答 5

6

自定义导航栏的外观

可以修改 barStyle、tintColor 和 translucent 属性,但绝对不能直接更改 UIView 级别的属性,例如 frame、bounds、alpha 或 hidden 属性。

有关更多详细信息,您可以关注苹果文档UINavigationBar 类参考

编辑->

我解决了你的问题

[[UINavigationBar appearance] addSubview:yourView];  

其他导航相关查询

于 2012-12-15T07:36:30.490 回答
0

试试这哥们...

//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];

让我知道它是否有效!!!!

快乐编码!!!

于 2012-12-15T06:53:15.290 回答
0

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

  yourViewController *mainVC = [[yourViewController alloc]init];
  UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:mainVC];
  navigationController1.title = @"title";
[window addSubview:navigationController1.view];
于 2012-12-15T06:53:47.217 回答
0

如果你使用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];
    }
} 

我想这是一个权利,因为我没有实施。

于 2012-12-15T07:36:28.193 回答
0

我想在每个 NavigationController 中添加一个徽标,所以我创建了一个子类 UINavigationController并在viewDidLoad-Method中使用它

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];
于 2013-08-30T14:21:49.507 回答