【问题标题】:Iphone/Ipad Add Image and Text to Navigation TitleIphone/Ipad 将图像和文本添加到导航标题
【发布时间】:2010-12-10 18:05:15
【问题描述】:

我只是想向 navigationItem 标题添加图像和文本。这就是我正在做的,但它只显示图像而不是标题。这似乎很容易。

示例 (

self.navigationItem.titleView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo_small_white.png"]] autorelease];
self.navigationItem.title = @"Company";

【问题讨论】:

标签: iphone ios uiimage uinavigationbar uinavigationitem


【解决方案1】:

这是我的代码:

UIView *myView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 300, 30)]; 
UILabel *title = [[UILabel alloc] initWithFrame: CGRectMake(40, 0, 300, 30)];

title.text = NSLocalizedString(@"My Title", nil);
[title setTextColor:[UIColor whiteColor]];
[title setFont:[UIFont boldSystemFontOfSize:20.0]];

[title setBackgroundColor:[UIColor clearColor]];
UIImage *image = [UIImage imageNamed:@"MyLogo.png"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:image];

myImageView.frame = CGRectMake(0, 0, 30, 30); 
myImageView.layer.cornerRadius = 5.0;
myImageView.layer.masksToBounds = YES;
myImageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
myImageView.layer.borderWidth = 0.1;

[myView addSubview:title];
[myView setBackgroundColor:[UIColor  clearColor]];
[myView addSubview:myImageView];
self.navigationItem.titleView = myView;

【讨论】:

    【解决方案2】:

    通过设置自定义标题视图,您将替换通常显示标题文本的标准视图。来自UINavigationItemtitleView 的文档:

    如果您将此属性设置为自定义标题,则会显示它而不是标题。

    所以你需要添加你自己的视图(可能是UILabel)来显示你的标题——一种方法是添加一个UILabel和上面的UIImageView作为容器UIView的子视图,并将 that 设置为 titleView

    【讨论】:

      【解决方案3】:

      Swift 2 中:

      var myView: UIView = UIView(frame: CGRectMake(0, 0, 300, 30))
      var title: UILabel = UILabel(frame: CGRectMake(40, 0, 300, 30))
      title.text = "The Title"
      title.textColor = UIColor.blackColor()
      title.font = UIFont.boldSystemFontOfSize(20.0)
      title.backgroundColor = UIColor.clearColor()
      var image: UIImage = UIImage(named: "your-image")!
      var myImageView: UIImageView = UIImageView(image: image)
      myImageView.frame = CGRectMake(0, 0, 30, 30)
      myImageView.layer.cornerRadius = 5.0
      myImageView.layer.masksToBounds = true
      myImageView.layer.borderColor = UIColor.lightGrayColor().CGColor
      myImageView.layer.borderWidth = 0.1
      myView.addSubview(title)
      myView.backgroundColor = UIColor.clearColor()
      myView.addSubview(myImageView)
      
      self.navigationItem.titleView = myView
      

      相关问题:titleView 自动居中,如果上面的示例不是这种情况,那么您可能使用的是较小的屏幕尺寸(例如 iPhone)。只需根据需要将 UIView 和 UILabel 的宽度从 300 更改为更小。有空间时它会弹回中心。

      【讨论】:

        【解决方案4】:
        UIImage *image = [UIImage imageNamed:@"image.png"]; // Image name with extension
        
        self.navigationItem.titleView = [[[UIImageView alloc] initWithImage:image] autorelease];
        

        这可能是最短的方法。

        【讨论】:

        • 最短的是 self.navigationItem.titleView = [UIImageView.alloc initWithImage:[UIImage imageNamed:@"image.png"]];
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-05
        相关资源
        最近更新 更多