简体   繁体   中英

How to Add UIImageView with UILabel in navigation bar of iphone?

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 ... 在此处输入图片说明

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..

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM