2012-01-13 69 views
0

我做了一个iPad应用程序,在我的导航控制的标题,图像中目标C导航栏

现在

标题栏,我希望把图像在左边,所以我隐藏标题栏用标签,但标签不覆盖标题栏的整个宽度

IL APP在屏幕SHOT,

这里是代码片段,

UILabel *titleView = (UILabel *)self.navigationItem.titleView; 
    if (!titleView) { 
     titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 800, 50)]; 
     titleView.backgroundColor = [UIColor clearColor]; 
     titleView.font = [UIFont boldSystemFontOfSize:20.0]; 
     titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]; 

     titleView.textColor = [UIColor yellowColor]; // Change to desired color 


     titleView.backgroundColor=[UIColor blackColor]; 

     titleView.text = @"IL APP"; 
     titleView.textAlignment=UITextAlignmentCenter; 
     //self.navigationItem.titleView = titleView; 
     [self.navigationItem setTitleView:titleView]; 
     [titleView release]; 
    } 

screen

+0

我不明白你的预期结果是什么。你能解释一下吗? – 2012-01-13 06:05:23

+0

在标题栏中,我用黄色书写了IL APP,我想将图像放在那里,在左侧.... – GAMA 2012-01-13 06:08:43

+0

您必须在导航栏的左侧显示标签? – Hiren 2012-01-13 06:14:05

回答

3

@Gama至于你的问题而言,你是要求把一个形象,但被描述你真正的问题是,标签不包含标题栏。为了覆盖正常,你可以使用

UILabel *titleView = (UILabel *)self.navigationItem.titleView; 
if (!titleView) { 
    titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 32)]; 
    titleView.backgroundColor = [UIColor clearColor]; 
    titleView.font = [UIFont boldSystemFontOfSize:20.0]; 
    titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]; 
    titleView.textColor = [UIColor yellowColor]; // Change to desired color 
    //titleView.backgroundColor=[UIColor blackColor]; 
    titleView.text = @"IL APP"; 
    titleView.textAlignment=UITextAlignmentCenter; 
    [self.navigationItem setTitleView:titleView]; 
    [titleView release]; 
} 
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]]; 

对于使用图像您可以使用图像视图,而不是一个标签被指定为titleview的为navigationItem。

希望它可以帮助

+0

老兄,它工作正常,你摇滚,现在我想把图像放在我的导航栏中,我应该怎么做? – GAMA 2012-01-13 06:28:24

+0

你要在哪里放置图像,创建一个具有所需图像的UIImageView并将其分配给titleView,就像指定了标签一样。如果你想使用自定义图像的左或右栏按钮,那么你可以使用UIBarButton的initWithImage:方法 – 2012-01-13 09:56:54

+0

我能够把图像,但不幸它总是对齐在中心,而我想它从左侧开始和应该穿过标题栏。我创建了标题栏大小的图像(纵向视图),但是当应用程序加载时,它会从左右角落留下一些空间。同样在横向视图中,空间更宽(因为图像尺寸来自肖像)。我可以根据方向加载适当的图像,但想知道是否有更好的方法。 – GAMA 2012-01-13 11:08:56

0

我试图解决你的问题,但我得到了同样的结果。

但是你可以隐藏导航栏,并添加在UILabelViewController

self.navigationController.navigationBarHidden = FALSE; 
[self.view addSubview:titleview]; 

UIImageView *imageNav = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"navImage.png"]]; 
[self.navigationController.navigationBar addSubview:imagenav]; 

[self.navigationController.navigationBar sendSubviewToBack:imageNav]; 
[imageNav release]; 
+0

我试过了,self.navigationController.navigationBarHidden = TRUE;导航栏消失了,但是在我的新页面里没有后退按钮,回去后,我想要返回按钮。 – GAMA 2012-01-13 06:16:13

+0

在titleview中添加一个按钮看起来像后退按钮,并把按钮点击事件的弹出方法 – Hiren 2012-01-13 06:18:35

+0

我试图在我的新导航里面我的负载方法,self.navigationController.navigationBarHidden = FALSE ;,我得到的按钮,但是当我点击后退按钮,它来到主页,但与导航栏 – GAMA 2012-01-13 06:22:07

0

嗨,我想做同样的事情,我看到你的帖子,我基本上使用了一些您所提供的代码,并修改了一些,得到了它的工作见下文:

//declare and initialize your imageView 
UIImageView *titleImage = (UIImageView *)self.navigationItem.titleView; 

titleImage = [[UIImageView alloc] 
initWithFrame:CGRectMake((self.navigationController.navigationBar.frame.size.width/2) 
- (100/2), 0, 100, self.navigationController.navigationBar.frame.size.height)]; 

//setting the image for UIImageView 
titleImage.image = [UIImage imageName:@"photo.jpg"]; 

//注意:你必须在底部做这一行才能将图像添加到导航栏试试看!

self.navigationItem.titleView = titleImage;