为所有navigationcontroller和navigationitem设置titleview

时间:2012-12-15 06:42:18

标签: iphone objective-c ios uinavigationcontroller uinavigationbar

[[[UINavigationBar appearance] setTitleView:button]

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

5 个答案:

答案 0 :(得分:6)

自定义导航栏的外观

  

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

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

修改 - >

我解决了你的问题

[[UINavigationBar appearance] addSubview:yourView];  

其他Navigation Related query

答案 1 :(得分: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];

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

快乐编码!!!

答案 2 :(得分:0)

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

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

答案 3 :(得分: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];
    }
} 

我想这是对的,因为我没有实施。

答案 4 :(得分:0)

我想在每个NavigationController中添加一个徽标,所以我创建了 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];