Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何在uinavigationbar中的特定位置添加项目?_Iphone_Ios_Uinavigationbar_Uinavigationitem_Navigationbar - Fatal编程技术网

Iphone 如何在uinavigationbar中的特定位置添加项目?

Iphone 如何在uinavigationbar中的特定位置添加项目?,iphone,ios,uinavigationbar,uinavigationitem,navigationbar,Iphone,Ios,Uinavigationbar,Uinavigationitem,Navigationbar,我正在为iphone开发一个应用程序,在特定位置的导航栏中添加多个UILabel时,我遇到了一些问题 我想要的是下面的图片 上图中有一个backbar按钮和一个imageview,如箭头所示。除此之外,所有白色框用于difrentUILabels在导航栏中显示。 导航栏中只有一个UIImageView,还有一个左栏按钮。现在的问题是,我不知道如何在导航栏的特定位置添加多个UILabel 到目前为止,我所做的工作是使用右栏按钮数组或左栏按钮数组只添加按钮和UIImageView,但这只添加seq

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

我想要的是下面的图片

上图中有一个backbar按钮和一个
imageview
,如箭头所示。除此之外,所有白色框用于difrent
UILabels
在导航栏中显示。 导航栏中只有一个
UIImageView
,还有一个左栏按钮。现在的问题是,我不知道如何在导航栏的特定位置添加多个
UILabel

到目前为止,我所做的工作是使用右栏按钮数组或左栏按钮数组只添加按钮和
UIImageView
,但这只添加sequence中的项目


因此,任何人都可以指导我如何在特定位置添加UILabel或任何其他项目。

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

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

标签框原点将相对于导航栏

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

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()
中的值即可满足您的需要

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

myViewController.navigationItem.titleView = someView;

请注意,视图的框架很可能会被限制为
leftBarButtonItem
rightBarButtonItem

您可以添加多个UIlabel或图像,如下图所示:-

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

这可以通过使用下面的代码来实现,您可以根据需要更改标签和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;

}
试试这个代码

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

太棒了,如果问题解决了,我建议接受回答Apple不建议直接添加视图作为导航栏的子视图。我同意,他们通常不喜欢触摸任何本机控制器。无论如何,这只是一种方法:)