2013-07-12 78 views
4

上的按鈕,扣帽子,我已創建自定義導航器的導航欄上的iOS

我想加入,在左邊的日期,在右邊,旁邊後退按鈕標題後退按鈕。

我試圖添加一個標籤,但它不起作用。請告訴我一個方法

UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 66)]; 


UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,8,280,30)]; 
navLabel.text = @"My Text"; 

[self.navigationController.navigationBar addSubview:navLabel]; 

[self.view addSubview:naviBarObj]; 
+0

要小心,在你試圖改變'naviController.navigationBar'的標題而不是'naviBarObj'的代碼。 – alexbumbu

回答

6
UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 66)]; 

    UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(200,8,200,30)]; 
    navLabel.text = @"My Text"; 
    navLabel.textColor = [UIColor redColor]; 
    [naviBarObj addSubview:navLabel]; 
    [navLabel setBackgroundColor:[UIColor clearColor]]; 
    [self.view addSubview:naviBarObj]; 

試試這個它會工作。它爲我工作:)

+0

如何使用Swift 3? –

0

您需要更改topItemtitleView。就像這樣:

naviBarObj.topItem.titleView = navLabel; 

**更新:完整代碼(測試工作)**

UINavigationBar *naviBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(.0, .0, 320.0, 44.0)]; 
UINavigationItem *navItem = [[UINavigationItem alloc] init]; 
navItem.titleView = titleLbl; 

[naviBar pushNavigationItem: navItem animated: NO]; 
[navItem release]; // you don't need this for ARC 

[self.view addSubview: naviBar]; 
[naviBar release]; // you don't need this for ARC 
+0

雖然 – erdemgc

+0

沒有出現,但很可能您需要在更新之前將naviBarObj添加到naviBarObj,例如發佈了@Eimantas。 – alexbumbu

0

您需要導航項目添加到導航欄。每個導航級別一個。所以你應該至少有一個。

**更新**

UINavigationItem * myNavItem = [[UINavigationItem alloc] initWithTitle:@"Some title"]; 

/* 
* add buttons and labels 
*/ 

[self.navigationController.navigationBar pushNavigationItem:myNavItem animated:NO]; 
+0

我無法添加, self.navigationcontroller中沒有addSubView方法 – erdemgc

2

加入UIToolbar自定義視圖

UIToolbar *tools = [[UIToolbar alloc] 
         initWithFrame:CGRectMake(0.0f, 0.0f,190.0f, 44.01f)]; 

    tools.barStyle = -1; // clear background 
    NSMutableArray *buttons = [[NSMutableArray alloc] init]; 

    UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; 
    bi.width =10.0f; 
    [buttons addObject:bi]; 
    [buttons addObject:add your view]; 
    bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; 
    bi.width =10.0f; 
    [buttons addObject:bi]; 
    [buttons addObject:add your view]; 
    [tools setItems:buttons animated:NO]; 
    UIBarButtonItem *buttons_ = [[UIBarButtonItem alloc] initWithCustomView:tools]; 
    self.navigationItem.rightBarButtonItem = buttons_; //same to leftBarButtonItem