2

我正在为 iphone 开发一个应用程序,并且UILabel在导航栏中的特定位置添加多个应用程序时遇到了一些问题。

我想要的是以下图片

在此处输入图像描述

在上图中,有一个后退栏按钮和一个imageview带有箭头标记的按钮。除此之外,所有白框都是为了UILabels在导航栏中显示不同的内容。UIImageView导航栏中只有一个,左栏按钮只有一个。现在的问题是我不知道如何UILabel在导航栏中的特定位置添加多个。

到目前为止,我所做的只是添加按钮并UIImageView使用右栏按钮数组或左栏按钮数组,但这只会按顺序添加项目。

那么任何人都可以指导我,任何人都可以在特定位置添加 UILabels 或任何其他项目..

4

6 回答 6

7

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;

}
于 2013-06-06T10:14:37.813 回答
2

您可以将子视图直接添加到导航栏 -

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

yourLabel 框架原点将相对于导航栏

于 2013-06-06T09:52:48.553 回答
2

试试这个代码

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];
于 2013-06-06T10:43:43.963 回答
1

看看这段代码。通过一些修改,它应该可以解决您的问题:

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

只需修改 中的值CGRectMake()以满足您的需要。

于 2013-06-06T09:53:20.630 回答
1

您可以设置任意视图而不是视图控制器的标题:

myViewController.navigationItem.titleView = someView;

请注意,视图的框架很可能会被限制为leftBarButtonItem和腾出空间rightBarButtonItem

于 2013-06-06T09:53:44.130 回答
1
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];
于 2013-06-06T10:01:44.340 回答