How to Add UIImageView with UILabel in navigation

2019-04-15 01:44发布

I have some navigation bar in my UI of iphone app. Now i want to add two images on the navigation bar on center of the navigation bar.. I have successfully added the two UIImageView on the navigation-bar but my problem is that i have two UIImageView with the label on it ..

Let me show u the snapshot ...enter image description here

now my problem is i don't know how to add this kind of label near UIImageView in navigation bar..so please if anyone can guide me for this then it will help me a lot..

i have already successfully inserted two images on navigation bar with this Code.

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

Please Guide me as soon as possible..

3条回答
\"骚年 ilove
2楼-- · 2019-04-15 01:55

I think this would help you.

 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楼-- · 2019-04-15 02:00

possible duplicate. see here. It seems you need to inherit UIBarButtonItem and create your own custom view with a UIButton with a subview of the badge you need to add. Hope it helps.

查看更多
4楼-- · 2019-04-15 02:03

Please use this

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

Please use this code by setting the frame according to you images. Because you have already added images to your project, just put the label next to them wherever you want.

Hope this helps.

查看更多
登录 后发表回答