2013-06-06 63 views
2

我正在開發一個應用程序的iPhone和我有一些問題有關在導航欄中添加一個以上的一個特定位置的UILabel如何在uinavigationbar中的特定位置添加項目?

我想是在下面的圖像

enter image description here

在上述圖像

有一個backbar按鈕和一個imageview如圖箭頭標記。除此之外,所有的白色方框都是爲了在導航欄中顯示不同的UILabels。 導航欄中只有一個UIImageView和一個左側欄按鈕。現在的問題是,我不知道如何在導航欄中添加多個UILabel特定位置。

直到現在我的工作是添加只有按鈕和UIImageView與正確的酒吧按鈕陣列或左欄按鈕數組,但只有添加項目的sequnce。

所以,任何人都可以請指導我怎麼可能有人在某個特定位置添加UILabels或任何其他項目..

回答

7

您可以添加多個的UILabel或圖片作爲像波紋管圖像: -

enter image description here

這可怎麼辦使用波紋管代碼,你可以改變標籤和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; 

} 
2

您可以直接添加子視圖中的導航欄 -

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

yourLabel框架原點將相對於導航條

+0

thx ..我剛剛得到我想要的 –

+0

真棒,如果問題關閉我建議接受答案 –

+0

Apple不建議直接添加視圖作爲導航欄的子視圖。 – Adithya

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()的值,以滿足您的需求。

1

您可以設置任意視圖,而不是一個視圖控制器的標題:

myViewController.navigationItem.titleView = someView; 

注意,最有可能的視圖的框架將被限制,以騰出空間leftBarButtonItemrightBarButtonItem

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]; 
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]; 
相關問題