繁体   English   中英

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

[英]Objective C how to add textfield into navigation bar

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

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

有任何想法吗?

谢谢!

我以这种方式做到了。 我知道您现在已经回答了这个问题,但这仍然对其他人有帮助,因此可以按照以下步骤进行操作

  -(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];
  }

谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM