2013-02-04 44 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;