2010-06-18 56 views
2

我试图将文本字段添加到导航栏中,但它不显示在模拟器中。我做如下viewdidload内:Objective C如何将文本字段添加到导航栏中

UITextView *textField = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 170, 44)]; 
self.navigationItem.titleView = textField; 

任何想法?

谢谢!

+0

您是否添加了一些文字? – typeoneerror 2010-06-18 22:52:17

+0

我没有添加任何文字。我想覆盖它与搜索图标(放大镜),但无法弄清楚,如果我可以做到这一点,而无需添加PNG图标。默认的搜索栏更圆,但我希望它看起来更像一个文本字段。 谢谢! David – prostock 2010-06-19 07:25:04

回答

3

我已经这样做了。我知道你已经得到的答案uptil现在 但仍这将有助于别人这样 这是可以做到如下

-(void)viewWillAppear:(BOOL)animated 
    { 
    imgVw =[[UIImageView alloc]initWithFrame:CGRectMake(68, 7, 180, 28)]; 
    [imgVw setImage:[UIImage imageNamed:@"textfieldBackground.png"]]; 
    [self.navigationController.navigationBar addSubview:imgVw]; 

     textfieldTxt = [[UITextField alloc]initWithFrame:CGRectMake(78, 11, 170, 25)]; 
     textfieldTxt.backgroundColor = [UIColor clearColor]; 
     [textfieldTxt setAutocorrectionType:UITextAutocorrectionTypeNo]; 
     textfieldTxt.delegate = self; 
     [self.navigationController.navigationBar addSubview:textfieldTxt]; 

    } 

不要忘了从上海华盈,除去viewdidunload时请

- (void)viewDidUnload 
    { 
    [super viewDidUnload]; 
    [textfieldTxt removeFromSuperview]; 
    [imgVw removeFromSuperview]; 
    } 

谢谢

+0

这很糟糕,因为'navigationController.navigationBar'不属于当前视图控制器。你最好把它放到一个自定义栏按钮或标题视图中。 http://www.iphonegamezone.net/118/ – fabb 2015-01-08 10:26:55

相关问题