how to add items in uinavigationbar at a specific

2019-01-28 21:52发布

I am developing one application for iphone and in that i have some issues regarding adding more then one UILabel in navigation bar at a specific position.

What i want is in following images

enter image description here

in above image there is one backbar button and one imageview as shown with arrow-mark. Other then that all white box is for difrent UILabels to show in navigation bar. There is only one UIImageView in the navigation bar and one left bar button. Now problem is that i don't know how to add multiple UILabel in navigation bar at a specific position.

Till now what i have worked is to add only button and UIImageView with the right bar button array or left bar button array, but that only add items in sequnce.

So can anyone please guide me that how can anyone add UILabels or any other item at a specific position..

6条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-28 22:05

You can set an arbitrary view instead of a view controller's title:

myViewController.navigationItem.titleView = someView;

Note that most likely the view's frame will be restricted to make room for leftBarButtonItem and rightBarButtonItem.

查看更多
SAY GOODBYE
3楼-- · 2019-01-28 22:09

Try this code

UIView *buttonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
buttonContainer.backgroundColor = [UIColor clearColor];
UIButton *button0 = [UIButton buttonWithType:UIButtonTypeCustom];
   [button0 setFrame: CGRectMake(160, 7 , 40,  30)];
[button0 setTitle:@"Sort" forState:UIControlStateNormal];
button0.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:12];
[button0 setBackgroundImage:[UIImage imageNamed:@"Btn_Sort_Btn_Sort_Places.png"]    forState:UIControlStateNormal];
[button0 addTarget:self action:@selector(sortAction) forControlEvents:UIControlEventTouchUpInside];
[button0 setShowsTouchWhenHighlighted:YES];
[buttonContainer addSubview:button0];
self.navigationItem.titleView = buttonContainer;

UILabel *lbl_title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0 , 140, 40)];
lbl_title.text = str_HeaderTitle;
lbl_title.textColor = [UIColor whiteColor];
lbl_title.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
lbl_title.backgroundColor = [UIColor clearColor];
lbl_title.textAlignment   = NSTextAlignmentCenter;
[buttonContainer addSubview:lbl_title];
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-28 22:09

Have a look at this piece of code. With some modifications it should solve your problem:

UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[imageButton setFrame:CGRectMake(15, 0, 57, 44)];
[imageButton setBackgroundImage:[UIImage imageNamed:@"someImage.png"] forState:UIControlStateNormal];


UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 0, 250, 44)];
[titleLabel setText:@"some title"];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setFont:[UIFont boldSystemFontOfSize:20]];

[self.navigationController.navigationBar addSubview:imageButton];
[self.navigationController.navigationBar addSubview:titleLabel];

Just modify the values in the CGRectMake() to fit your needs.

查看更多
祖国的老花朵
5楼-- · 2019-01-28 22:11

you can add Multiple UIlabel or Image as look like bellow image:-

enter image description here

this can do using bellow code you can change Label and imageView frame and put as your requirement

- (void)viewDidLoad
{

    UIView *btn = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];

    UILabel *label;
    label = [[UILabel alloc] initWithFrame:CGRectMake(5, 25, 200, 16)];
    label.tag = 1;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:16];
    label.adjustsFontSizeToFitWidth = NO;
    label.textAlignment = UITextAlignmentLeft;
    label.textColor = [UIColor whiteColor];
    label.text = @"My first lable";
    label.highlightedTextColor = [UIColor whiteColor];
    [btn addSubview:label];
    [label release];

    label = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 200, 16)];
    label.tag = 2;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:16];
    label.adjustsFontSizeToFitWidth = NO;
    label.textAlignment = UITextAlignmentRight;
    label.textColor = [UIColor whiteColor];
    label.text = @"second line";
    label.highlightedTextColor = [UIColor whiteColor];
    [btn addSubview:label];
    [label release];


    UIImageView *imgviw=[[UIImageView alloc]initWithFrame:CGRectMake(170, 10, 20, 20)];
     imgviw.backgroundColor = [UIColor blackColor];
    imgviw.image=[UIImage imageNamed:@"a59117c2eb511d911cbf62cf97a34d56.png"];
     [btn addSubview:imgviw];

    self.navigationItem.titleView = btn;

}
查看更多
放我归山
6楼-- · 2019-01-28 22:19

You can add subviews directly to the navigationBar -

UINavigationBar *navBar = navController.navigationBar;
[navBar addSubview: yourLabel];

yourLabel frame origin will be relative to the navigation bar

查看更多
够拽才男人
7楼-- · 2019-01-28 22:26
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(50, 2, 20, 15)];
[label setBackgroundColor:[UIColor greenColor]];

UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(40, 20, 150, 10)];
[imgView setBackgroundColor:[UIColor redColor]];

[[self.navigationController navigationBar] addSubview:label];
[self.navigationController.navigationBar addSubview:imgView];
查看更多
登录 后发表回答