我正在使用UITextField接收用戶輸入。但是,由於我的文本字段位於筆尖的中間/底部,因此彈出鍵盤時會隱藏它。是否有任何方式將其與鍵盤一起滑動,以便在選擇過程中位於鍵盤的頂部?另外,因爲我也使用數字鍵盤,有沒有簡單的方法在某處包含完成按鈕?iPhone - 鍵盤隱藏TextField
感謝您的幫助。
我正在使用UITextField接收用戶輸入。但是,由於我的文本字段位於筆尖的中間/底部,因此彈出鍵盤時會隱藏它。是否有任何方式將其與鍵盤一起滑動,以便在選擇過程中位於鍵盤的頂部?另外,因爲我也使用數字鍵盤,有沒有簡單的方法在某處包含完成按鈕?iPhone - 鍵盤隱藏TextField
感謝您的幫助。
要在鍵盤出現時滾動,我喜歡Cocoa With Love中的this tutorial。
要關閉數字小鍵盤,您可以在屏幕的其餘部分使用put a custom "Done" button on the keypad或隱藏按鈕。我用代碼完成了後者,但this tutorial使用Interface Builder。
第三個鏈接是壞的:( – osdamv
基於在這個答案的第一個教程鏈接我想出了這個要點:https: //gist.github.com/siannopollo/7892526它會執行更少且更一致的滾動 – siannopollo
您必須手動執行此操作。看看這個答案。 UITextField: move view when keyboard appears
這種情況在iPhone上很爛。你應該做的是設計你的界面,使得當鍵盤出現時字段可見,或者當鍵盤出現時將字段向上移動。 (當你完成後退後)
我建議看一些蘋果應用程序,如聯繫人或設置,看看他們是如何處理這個。
此外,我敢肯定iPhone HIG有話要說。
我已經爲這個問題做了一個簡單的應用程序。它會自動檢查文本框的位置,如果鍵盤隱藏它,則會根據需要自動向上移動。
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField:textField up:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField:textField up:NO];
}
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
int animatedDistance;
int moveUpValue = textField.frame.origin.y+ textField.frame.size.height;
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = 216-(460-moveUpValue-5);
}
else
{
animatedDistance = 162-(320-moveUpValue-5);
}
if(animatedDistance>0)
{
const int movementDistance = animatedDistance;
const float movementDuration = 0.3f;
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: nil context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
我在我的第8個文本框中,我點擊了tabbar,就像是一個完整的視圖消失了,爲什麼? – Dee
@Dee請給我代碼。 。我可以查看並回復你。 – ValayPatel
如果你正在使用這個代碼,使用'[UIView transitionWithView ...]'而不是'[UIView beginAnimations ...],[UIView setAnimationBegin ...],[UIView setAnimationDuration ...],[UIView commitAnimations]。' – DevC
使用此代碼:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:self.view.window];
}
- (void)viewDidDisappear:(BOOL)animated {
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidHideNotification
object:nil];
}
- (void)keyboardDidHide:(NSNotification *)n
{
CGRect viewFrame = scrollView.frame;
UIDeviceOrientation orientSide = [[UIDevice currentDevice] orientation];
if ((orientSide == UIDeviceOrientationLandscapeRight) || (orientSide == UIDeviceOrientationLandscapeLeft))
viewFrame.size.height += 140;
else viewFrame.size.height += 216; //portrait mode
scrollView.frame = viewFrame;
keyboardVisible = NO;
}
- (void)keyboardDidShow:(NSNotification *)n
{
CGRect viewFrame = scrollView.frame;
UIDeviceOrientation orientSide = [[UIDevice currentDevice] orientation];
if ((orientSide == UIDeviceOrientationLandscapeRight) || (orientSide == UIDeviceOrientationLandscapeLeft))
viewFrame.size.height -= 140;
else viewFrame.size.height -= 216; //portrait mode
scrollView.frame = viewFrame;
keyboardVisible = YES; }
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
CGRect viewFrame = scrollView.frame;
if ((interfaceOrientation == UIDeviceOrientationLandscapeRight) || (interfaceOrientation == UIDeviceOrientationLandscapeLeft)) //wants to change to landscape mode
if (keyboardVisible == NO)//currently in portrait,check if keyboard was present
viewFrame.size = CGSizeMake(480,250);
else viewFrame.size = CGSizeMake(480,170);
else {//wants to change to portrait mode
if (keyboardVisible == NO)//currently in landscape,check if keyboard was present
viewFrame.size = CGSizeMake(320,416);
else viewFrame.size = CGSizeMake(320,200);
}
scrollView.frame = viewFrame;
return YES;
}
Works爲風景模式太多,但僅支持iPhone。對於iPad,請相應更改相框設置。當按下返回時,textFieldShouldReturn:
方法將使鍵盤隱藏。希望這可以幫助...
什麼是scrollView參數? – Dejell
這是您的scrollView的名稱,其中包含文本字段。 – tipycalFlow
這可能會導致問題,因爲viewDidUnload永遠不會在iOS 6中進一步調用,因此實際上不會取消註冊監聽器。 – ljboy
我知道我在回答這個問題時有點晚,但是,我發現了一個非常簡單的解決方案。如果您使用UITableView控制器而不是具有tableView作爲對象的UIViewController,則不會出現此問題。
當您單擊表格底部的文本字段時,彈出鍵盤,表格視圖自動向上滾動,直到正在編輯的文本字段可見。
但是,當我們使用鍵盤上的返回按鈕時,自動滾動不起作用。在這種情況下,我們手動向上滾動表格以查看文本字段。
我希望這有助於
我一直在尋找類似問題的無數答案。對於基本的單屏應用程序,這種解決方案的作用像一個魅力,並且實現起來非常簡單:https://github.com/michaeltyson/TPKeyboardAvoiding
當然,有更多優雅的解決方案,但這樣可以使工作更快捷。
將UITableView設置爲編輯模式非常簡單!
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView setEditing:YES];
}
如果你想隱藏刪除bubbels,到單元格的左側,然後實現的UITableViewDelegate方法:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"%f",textField.frame.origin.y);
CGPoint scrollPoint = CGPointMake(0, textField.frame.origin);
[scrollView setContentOffset:scrollPoint animated:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[scrollView setContentOffset:CGPointZero animated:YES];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField:textField up:YES];
}
請在您的視圖控制器使用此代碼.m文件..使用此代碼時,您單擊文本字段的鍵盤將出現。再次點擊您的視圖時,鍵盤將被隱藏..我希望這可以幫助你...
,如果有人需要它,i`ve翻譯ValayPatel來迅速
func animatedTextField(textField: UITextField, up: Bool){
var animatedDistance : Int = 0
let moveUpValue : Int = Int(textField.frame.origin.y) + Int(textField.frame.size.height)
switch UIDevice.currentDevice().orientation {
case .Portrait , .PortraitUpsideDown:
animatedDistance = 216 - (460 - moveUpValue - 5)
default:
animatedDistance = 162 - (320 - moveUpValue - 5)
}
if (animatedDistance > 0){
let movementDistance : Int = animatedDistance
let movementDuration : Float = 0.3
let movement = up ? -movementDistance : movementDistance
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(NSTimeInterval(movementDuration))
self.view.frame = CGRectOffset(self.view.frame, 0, CGFloat(movement))
UIView.commitAnimations()
}
}
請將最後一行改爲'self.view.frame = self.view.frame.offsetBy(dx:0,dy:CGFloat(movement))' –
解決方案這是可能的欺騙:http://stackoverflow.com/questions/1775860/uitextfield-move-view-when鍵盤 - 但是我不是iPhone的開發者,所以會向社區退出來決定。 – Kev
這可能是一個解決方案給你:http://stackoverflow.com/a/17707278/1582217 –