目标c中导航栏标题中的图像

时间:2012-01-13 05:57:59

标签: iphone ios ipad uinavigationbar title

我制作了一个iPad应用程序,因为我使用了导航控件,

现在在标题栏中,我想将图像放在左侧,所以我用标签隐藏标题栏,但标签不覆盖标题栏的整个宽度,

屏幕拍摄中的IL APP,

这是代码段,

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

3 个答案:

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

使用图像时,可以使用图像视图而不是标签作为navigationItem的titleView。

希望有所帮助

答案 1 :(得分:0)

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

但您可以隐藏导航栏并将UILabel添加到ViewController

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

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