2

我想用常規文本字段更改navigationBar的titleView。然後我想設置textField大小來填充舊的「正常」titleView。如何將導航欄的標題視圖更改爲某些自定義視圖並填充原始大小

我似乎無法在storyBoard中做到這一點。將文本字段拖動到導航標題View所在的位置不起作用。

所以我在

-(void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
    PO(self.navigationItem.titleView); 
    CGRect theFrame= self.navigationItem.titleView.frame; 
    self.navigationItem.titleView=self.searchBar; 
    //self.searchBar.frame = theFrame; 
    while (false); 
... 

添加的東西它的工作有一個緩存。 PO是一個打印對象內容的宏。在viewDidAppear發現,self.navigationItem.titleView爲null。

因此,雖然我可以顯示searchBar,但我無法讓searchBar「填充」它的空間,因爲我不知道空間是什麼。

我不想硬編碼它,因爲你知道,事情可能會在未來發生變化。

那我該怎麼辦?

我曾經看到代碼而不是設置self.navigationItem.titleView,你只需要添加子視圖。這種方法即使在viewDidAppear問題,self.navigationItem.titleView爲0

我添加了這些代碼:

CGRect theFrame= self.navigationItem.titleView.frame; 
CGRect theFrame2 = self.searchBar.frame; 
CGRect theFrame3 = self.navigationController.navigationItem.titleView.frame; 

而且,我不知道如何NSLog的結構價值,然而,theFrame和theFrame3都是0

回答

2

你可以試試這個裏面viewWillAppear中:

UIView *customTitleView = [[UIView alloc]initWithFrame:CGRectMake((320-210)/2, 0, 210, 50)]; 
customTitleView.backgroundColor = [UIColor clearColor]; 

//create your UITextField or UILabel or other view and add as subview of customTitleView 

self.navigationItem.titleView = customTitleView; 
+1

除非你有很好的理由,最好是設置吶** viewDidLoad:**中的vigationItem值,而不是viewWillAppear :.每次出現視圖時,我都會看到設置navigationItem.titleView的奇怪行爲。 –

+0

使用條件標誌。 – mattsven

相關問題