2013-02-04 48 views
1

如何使UINavigationBar標題爲用戶可編輯的,我試過將titleView設置爲文本框,但它看起來不一樣。它缺少輪廓或投影。我瞄準它看起來像默認的。使UINavigationBar標題爲用戶可編輯

這就是我在此刻實現:

_titleField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 26)]; 
[_titleField setDelegate:self]; 
_titleField.text = _bugDoc.data.title; 
_titleField.font = [UIFont boldSystemFontOfSize:20]; 
_titleField.textColor = [UIColor whiteColor]; 
_titleField.textAlignment = NSTextAlignmentCenter; 
self.navigationItem.titleView = _titleField; 
+0

您正在製作自定義導航欄? –

+0

@Manohar,不,我不是。只需尋找一種讓用戶能夠點擊navBar標題進行編輯的方法。截至目前,我有一個textField設置爲titleView,但它看起來不像它的正文。 –

+0

請檢查我的答案。根據您的要求設置您的框架。 –

回答

2
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self loadTitle]; 
} 
- (void)loadTitle 
{ 
    txtField_navTitle = [[UITextField alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x,7,self.view.bounds.size.width,31)]; 
    txtField_navTitle.delegate = self; 
    [txtField_navTitle setBackgroundColor:[UIColor clearColor]]; 
    txtField_navTitle.textColor = [UIColor whiteColor]; 
    txtField_navTitle.textAlignment = UITextAlignmentCenter; 
    txtField_navTitle.borderStyle = UITextBorderStyleNone; 
    txtField_navTitle.font = [UIFont boldSystemFontOfSize:20]; 
    txtField_navTitle.autocorrectionType = UITextAutocorrectionTypeNo; 
    txtField_navTitle.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
    [self.navigationItem setTitleView:txtField_navTitle]; 
    txtField_navTitle.layer.masksToBounds = NO; 
    txtField_navTitle.layer.shadowColor = [UIColor whiteColor].CGColor; 
    txtField_navTitle.layer.shadowOpacity = 0; 
    [txtField_navTitle release]; 
} 
- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    [textField resignFirstResponder]; 
    self.title = textField.text; 
    return YES; 
} 

請不要忘記#import <QuartzCore/QuartzCore.h>

+0

這已經是我正在實施,但我在我的原始文章中說過,我有它的工作,但這種方法看起來不像原來的navBar標題看起來。 navBar標題欄最初具有像陰影或某種效果。 –

+0

你可以有這樣的效果。 –

+0

怎麼這麼?這就是我想要實現的。 –

0

嘗試這種方式這對我的作品。

// Custom Navigation Title. 

    UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 40)]; 
    tlabel.text=self.title; 
    tlabel.textAlignment=NSTextAlignmentCenter; 
    tlabel.font = [UIFont boldSystemFontOfSize:17.0]; 
    tlabel.textColor=[UIColor colorWithRed:7.0/255.0 green:26.0/255.0 blue:66.0/255.0 alpha:1.0]; 
    tlabel.backgroundColor =[UIColor clearColor]; 
    tlabel.adjustsFontSizeToFitWidth=YES; 
    self.navigationItem.titleView=tlabel;