将标题和图像添加到导航栏

时间:2011-03-26 11:17:36

标签: iphone image ios4 uinavigationbar

我需要将UIViewController的名称和图像设置到navigationBar。到目前为止,我能够显示图像,但标题当然不存在。

    // show image
    UIImage *image = [UIImage imageNamed: @"bar_icon.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
    imageView.frame = CGRectMake(0, 0, 40, 40);
    self.navigationItem.titleView = imageView;
    [imageView release];

感谢您的帮助,
BR,doonot

2 个答案:

答案 0 :(得分:8)

    // show image
    UIImage *image = [UIImage imageNamed: @"bar_icon.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
    imageView.frame = CGRectMake(70, 0, 40, 40);

    // label
    UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 0, 100, 40)];
    tmpTitleLabel.text = @"Mannschaft";
    tmpTitleLabel.backgroundColor = [UIColor clearColor];
    tmpTitleLabel.textColor = [UIColor whiteColor];

    CGRect applicationFrame = CGRectMake(0, 0, 300, 40);
    UIView * newView = [[[UIView alloc] initWithFrame:applicationFrame] autorelease];
    [newView addSubview:imageView];
    [newView addSubview:tmpTitleLabel];
    self.navigationItem.titleView = newView;

    [imageView release];
    [tmpTitleLabel release];

答案 1 :(得分:4)

创建一个带有2个子视图的UIView:UIImageView和UILabel。将titleView设置为UIView。

相关问题