【问题标题】:Add title and image to navigationbar将标题和图像添加到导航栏
【发布时间】:2011-03-26 11:17:35
【问题描述】:

我需要设置 UIViewController 的名称和导航栏的图像。到目前为止,我能够显示图像,但标题当然不见了。

    // 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,杜诺

【问题讨论】:

    标签: iphone image ios4 uinavigationbar


    【解决方案1】:
        // 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];
    

    【讨论】:

    • 有没有办法根据你给 UIImageView 的图像获取一个 Rect?如果你只想让矩形成为图像的大小,为什么你必须指定一个矩形?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2010-12-05
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-14
      相关资源
      最近更新 更多