1

如何使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;
4

2 回答 2

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>

于 2013-02-04T06:11:02.233 回答
-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;
于 2013-02-04T06:15:01.677 回答