[英]Add UITextField above keyboard iOS
在我的應用程序中,我正在嘗試在accessoryView上添加UITextField。 它應該如下工作:
我在網上看了一下,但我發現只在附件上添加按鈕的東西,我希望你能幫我找到一個在accessoryView上添加UITextField的解決方案。
謝謝
UPDATE
這就是我的意思:
我希望你能更好地理解我的問題,我不是在尋找一個應該用戶滾動視圖或其他東西的解決方案,我不想改變我的應用程序的界面......
添加一個視圖然后在它上面,請放置一個文本域並嘗試。 下面是代碼。
UIView * container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
[container setBackgroundColor:[UIColor redColor]];
UITextField * newTextfield = [[UITextField alloc] initWithFrame:CGRectMake(50, 0, 100, 44)];
[newTextfield setBackgroundColor:[UIColor yellowColor]];
[container addSubview:newTextfield];
self.textField.inputAccessoryView = container;
這包含一些顏色,只是為了區分和識別視圖。 根據您的需求進行更改和格式化。 它會工作。 :)
在.h
UIView *customtoolbar;
在.m中在viewdidload添加thi代碼
customtoolbar=[[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height+50, 320, 50)];`
在此之后添加此方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[self pressdone];
return YES;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
//216 is keyboard default height in portrait(162 in landscape)
[UIView animateWithDuration:0.7 animations:^{
[self addtoolbar:CGRectMake(0, self.view.frame.size.height-216-50, 320, 50)];
}];
return YES;
}
-(UIView *)addtoolbar:(CGRect )frame{
customtoolbar.frame=frame;
customtoolbar.backgroundColor=[UIColor darkGrayColor];
//give new frame to your textfield
txtfld.frame=CGRectMake(5,10, 220, 30);
[customtoolbar addSubview:txtfld];
UIButton *done=[UIButton buttonWithType:UIButtonTypeCustom];
done.frame=CGRectMake(235,10, 60, 30);
[done setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[done setTitle:@"Done" forState:UIControlStateNormal];
[done addTarget:self action:@selector(pressdone) forControlEvents:UIControlEventTouchUpInside];
[customtoolbar addSubview:done];
[self.view addSubview:customtoolbar];
return customtoolbar;
}
-(void)pressdone{
[self addtoolbar:CGRectMake(0, self.view.frame.size.height+50, 320, 50)];
//set there orignal frame of your textfield
txtfld.frame=CGRectMake(95, 170, 123, 37);
[self.view addSubview:txtfld];
[txtfld resignFirstResponder];
}
這就是我在我的應用程序中所做的
將觀察者添加到UIKeyboardWillHideNotification
和UIKeyboardWillShowNotification
當您查看通知時,請調整視圖keyboardFrame
大小
double duration = [[notification userInfo][UIKeyboardAnimationDurationUserInfoKey] doubleValue]; int curve = [[notification userInfo][UIKeyboardAnimationCurveUserInfoKey] intValue]; NSValue *value = [notification userInfo][UIKeyboardFrameEndUserInfoKey]; CGRect rawFrame = [value CGRectValue]; CGRect keyboardFrame = [self.view convertRect:rawFrame fromView:nil]; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:duration]; [UIView setAnimationCurve:curve];
//調整視圖大小
[UIView commitAnimations]
您可以在應用中使用滾動視圖並設置內容偏移量
- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.scroll.contentOffset = CGPointMake(0, textField.frame.origin.y);
}
我希望這與你的問題相關
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.