目标C如何将文本字段添加到导航栏中

时间:2010-06-18 22:39:52

标签: iphone cocoa-touch

我正在尝试将文本字段添加到导航栏中,但它不会显示在模拟器中。我在viewdidload中执行以下操作:

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

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

我用这种方式做到了。我知道你现在已经得到了答案 但这仍然会对其他人有所帮助 这可以按如下方式完成

  -(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

时,请不要忘记从superview中删除
 - (void)viewDidUnload
     {
     [super viewDidUnload];
     [textfieldTxt removeFromSuperview];
     [imgVw removeFromSuperview];
  }

感谢