如何添加项目在uinavigationbar在一个特定的位置?

我正在开发一个应用程序的iPhone和我有一些关于在一个特定的位置导航栏中添加一个以上的UILabel问题。

我想要的是在下面的图像

在这里输入图像说明

在上面的图像中,有一个背景button和一个图像视图,如箭头标记所示。 除此之外,所有的白色方块都是为了在导航栏中显示不同的UILabels 。 导航栏中只有一个UIImageView ,左边是一个栏button。 现在的问题是,我不知道如何添加多个UILabel在导航栏中的特定位置。

直到现在我所做的工作只是添加button和UIImageView与正确的酒吧button数组或左边的酒吧button数组,但只有添加项目sequnce。

所以任何人都可以请指导我如何添加UILabels或任何其他项目在特定的位置..

你可以添加多个UI标签或图像看起来像下图:

在这里输入图像说明

这可以使用波纹pipe代码,您可以更改标签和imageView框架,并把你的要求

 - (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; } 

你可以直接添加子视图到navigationBar –

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

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

试试这个代码

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

你可以设置一个任意的视图,而不是一个视图控制器的标题:

 myViewController.navigationItem.titleView = someView; 

请注意,最有可能的视图的框架将被限制为leftBarButtonItemrightBarButtonItem

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

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

 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()的值以适应您的需求。