Objective C如何添加textfield到导航栏
问题描述:
我正在将一个文本字段添加到导航栏,但它不会显示在模拟器中。我在viewdidload里面这样做:
I'm trying to add a textfield into a navigation bar, but it doesn't show up in the simulator. I do it as follows inside viewdidload:
UITextView *textField = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 170, 44)];
self.navigationItem.titleView = textField;
任何想法?
谢谢! / p>
Thanks!
答
我已经这样做了。我知道你已经得到了回答现在
,但仍然这将有助于别人所以
这可以做如下
i have done it in this way. i know that you have got to the answer uptil now but still this will helpful to someone else so this can be done as follows
-(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];
}
请不要忘记从superview中删除,当viewdidunload
please don't forgot to remove from superview, when viewdidunload
- (void)viewDidUnload
{
[super viewDidUnload];
[textfieldTxt removeFromSuperview];
[imgVw removeFromSuperview];
}
感谢