2010-02-21 141 views
21

我正在使用UITextField接收用户输入。但是,由于我的文本字段位于笔尖的中间/底部,因此弹出键盘时会隐藏它。是否有任何方式将其与键盘一起滑动,以便在选择过程中位于键盘的顶部?另外,因为我也使用数字键盘,有没有简单的方法在某处包含完成按钮?iPhone - 键盘隐藏TextField

感谢您的帮助。

+0

解决方案这是可能的欺骗:http://stackoverflow.com/questions/1775860/uitextfield-move-view-when键盘 - 但是我不是iPhone的开发者,所以会向社区退出来决定。 – Kev

+0

这可能是一个解决方案给你:http://stackoverflow.com/a/17707278/1582217 –

回答

16

要在键盘出现时滚动,我喜欢Cocoa With Love中的this tutorial

要关闭数字小键盘,您可以在屏幕的其余部分使用put a custom "Done" button on the keypad或隐藏按钮。我用代码完成了后者,但this tutorial使用Interface Builder。

+1

第三个链接是坏的:( – osdamv

+0

基于在这个答案的第一个教程链接我想出了这个要点:https: //gist.github.com/siannopollo/7892526它会执行更少且更一致的滚动 – siannopollo

1

这种情况在iPhone上很烂。你应该做的是设计你的界面,使得当键盘出现时字段可见,或者当键盘出现时将字段向上移动。 (当你完成后退后)

我建议看一些苹果应用程序,如联系人或设置,看看他们是如何处理这个。

此外,我敢肯定iPhone HIG有话要说。

26

我已经为这个问题做了一个简单的应用程序。它会自动检查文本框的位置,如果键盘隐藏它,则会根据需要自动向上移动。

 

- (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; 
} 
 
+0

我在我的第8个文本框中,我点击了tabbar,就像是一个完整的视图消失了,为什么? – Dee

+0

@Dee请给我代码。 。我可以查看并回复你。 – ValayPatel

+0

如果你正在使用这个代码,使用'[UIView transitionWithView ...]'而不是'[UIView beginAnimations ...],[UIView setAnimationBegin ...],[UIView setAnimationDuration ...],[UIView commitAnimations]。' – DevC

3

使用此代码:

- (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:方法将使键盘隐藏。希望这可以帮助...

+0

什么是scrollView参数? – Dejell

+0

这是您的scrollView的名称,其中包含文本字段。 – tipycalFlow

+0

这可能会导致问题,因为viewDidUnload永远不会在iOS 6中进一步调用,因此实际上不会取消注册监听器。 – ljboy

3

下面的代码适合我。

[textFieldFocused becomeFirstResponder]; 
[self.view addSubview:startView]; 
[textFieldFocused resignFirstResponder]; 
+0

可以请你在这里描述什么是** startView **? – swiftBoy

+0

startView只是另一种视图,它可以是任何视图。关键是becomeFirstResponder,然后辞职。 – idea2real

0

我知道我在回答这个问题时有点晚,但是,我发现了一个非常简单的解决方案。如果您使用UITableView控制器而不是具有tableView作为对象的UIViewController,则不会出现此问题。

当您单击表格底部的文本字段时,弹出键盘,表格视图自动向上滚动,直到正在编辑的文本字段可见。

但是,当我们使用键盘上的返回按钮时,自动滚动不起作用。在这种情况下,我们手动向上滚动表格以查看文本字段。

我希望这有助于

0

我一直在寻找类似问题的无数答案。对于基本的单屏应用程序,这种解决方案的作用像一个魅力,并且实现起来非常简单:https://github.com/michaeltyson/TPKeyboardAvoiding

当然,有更多优雅的解决方案,但这样可以使工作更快捷。

0

将UITableView设置为编辑模式非常简单!

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    [self.tableView setEditing:YES]; 
} 

如果你想隐藏删除bubbels,到单元格的左侧,然后实现的UITableViewDelegate方法:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 

    return NO; 
} 
0
- (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]; 
} 
0
- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    [self animateTextField:textField up:YES]; 
} 

请在您的视图控制器使用此代码.m文件..使用此代码时,您单击文本字段的键盘将出现。再次点击您的视图时,键盘将被隐藏..我希望这可以帮助你...

3

,如果有人需要它,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() 

    } 

} 
+0

请将最后一行改为'self.view.frame = self.view.frame.offsetBy(dx:0,dy:CGFloat(movement))' –