使UINavigationBar标题可由用户编辑

时间:2013-02-04 05:27:42

标签: iphone ios objective-c uinavigationbar

如何让UINavigationBar标题为用户可编辑,我尝试将titleView设置为文本字段,但它看起来不一样..它缺少轮廓或阴影。我的目标是看起来像默认的。

这就是我现在正在实施的内容:

 _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;

2 个答案:

答案 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>

答案 1 :(得分:-1)

尝试这种方式对我有用。

   // 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;