【问题标题】:How to Add UIImageView with UILabel in navigation bar of iphone?如何在 iPhone 的导航栏中添加带有 UILabel 的 UIImageView?
【发布时间】:2013-06-03 05:22:17
【问题描述】:

我的 iPhone 应用程序的UI 中有一些导航栏。现在我想在导航栏中心的导航栏上添加两个图像.. 我已成功在导航栏上添加了两个UIImageView,但我的问题是我有两个带有标签的UIImageView ..

让我给你看快照...

现在我的问题是我不知道如何在导航栏中的UIImageView 附近添加这种标签..所以如果有人可以指导我这样做,那么它将对我有很大帮助..

我已经使用此代码在导航栏上成功插入了两张图片。

   UIImageView *Messageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed :"message.png"]];
        Messageview.frame=CGRectMake(10, 0, 40, 40);

        UIImageView *BirthdayView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:"birthday.png"]];
        //titleView.frame=CGRectMake(60, 0, 50, 50);

UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

    [flexible setWidth:20];
    UIBarButtonItem *flexible1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

    [flexible setWidth:20];


        UIBarButtonItem *MessageBtn=[[UIBarButtonItem alloc]initWithCustomView:Messageview];

        UIBarButtonItem *BirthdayBtn=[[UIBarButtonItem alloc]initWithCustomView:BirthdayView];
        self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:rofileBtn,flexible,BirthdayBtn,MessageBtn,flexible1,nil];

请尽快指导我..

【问题讨论】:

  • 您在导航栏中设置自定义视图并设置默认导航栏隐藏
  • 为您的导航栏制作一个 customView---- 并设置默认导航栏 hiddenn...\
  • 我无法设置自定义视图..因为导航栏的左侧按钮必须始终存在,因为单击左侧按钮会打开一个侧栏...

标签: iphone ios xcode4.5 uinavigationbar uinavigationitem


【解决方案1】:

可能重复。见here。看来您需要继承 UIBarButtonItem 并使用 UIButton 创建您自己的自定义视图以及您需要添加的徽章的子视图。希望对您有所帮助。

【讨论】:

    【解决方案2】:

    我想这会对你有所帮助。

     UIView *msgView=[[UIView alloc]initWithFrame:CGRectMake(10, 0, 40, 40)];
        UIImageView *Messageview =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"message.png"]];
    
        Messageview.frame=CGRectMake(0, 0, 20, 20);
    
        [msgView addSubview:Messageview];
    
        UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(30, 0, 20, 20)];
        [lbl setText:@"Text"];
        [msgView addSubview:lbl];
    UIBarButtonItem *MessageBtn=[[UIBarButtonItem alloc]initWithCustomView:msgView];
    

    【讨论】:

      【解决方案3】:

      请用这个

      UILabel *labelforNavigationTitle = [[UILabel alloc] initWithFrame:CGRectZero];
      labelforNavigationTitle.font = [UIFont fontWithName:FONTSAVINGSBOND size:30.0];//SavingsBond
      labelforNavigationTitle.backgroundColor = [UIColor clearColor];  
      labelforNavigationTitle.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
      labelforNavigationTitle.textAlignment = UITextAlignmentCenter;
      labelforNavigationTitle.textColor = [UIColor whiteColor]; // change this color
      
      labelforNavigationTitle.text = NSLocalizedString(@"Text you want", @"");
      self.navigationItem.titleView = labelforNavigationTitle;
      [labelforNavigationTitle sizeToFit];
      

      请根据您的图像设置框架来使用此代码。因为您已经在项目中添加了图像,所以只需将标签放在它们旁边的任意位置即可。

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-15
        • 1970-01-01
        • 2019-11-03
        • 1970-01-01
        • 1970-01-01
        • 2022-08-08
        • 1970-01-01
        相关资源
        最近更新 更多