2013-08-02 73 views
0

讓我們假設我們有一個應用程序的登錄屏幕。登錄屏幕執行UITableViewController,其中包含UITableViewUITableView有1個部分,該部分包含兩個單元格(請參閱下面的故事板)。如何在編輯第一個UITextField時將UITableView滾動到第二個UITextField?

Storyboard

當前行爲

當用戶開始編輯電子郵件字段 - 與佔位符輸入你的電子郵件 - 鍵盤重疊密碼字段 - 與佔位符輸入密碼。

Password field is not visiblePassword field is visible when editing password

期望的行爲:

當用戶開始編輯電子郵件字段,鍵盤應該重疊既不場和鍵盤應該坐下低於這兩個字段。

嘗試性解決方案

我試圖用我的LoginFormController : UITableViewController以下迫使UITableView滾動到合適的位置,但沒有成功:

-(BOOL) textFieldSHouldBeginEditing:(RCTextField *)textField 
{ 
    [[self.view viewWithTag:textField.tag+1] becomeFirstResponder]; 

    // get the scroll index path of the last row in the first section 
    NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:1 inSection:0]; 

    // scroll the view there 
    [[self tableView] scrollToRowAtIndexPath:scrollIndexPath 
          atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 

    return YES; 
} 

LoginFormController : UITableViewController接口:

// 
// LoginFormController.h 
// Zealoushacker 
// 
// Created by Alex Notov on 7/31/13. 
// Copyright (c) 2013 Zealoushacker, Inc. All rights reserved. 
// 

#import <UIKit/UIKit.h> 

@interface LoginFormController : UITableViewController <UITextFieldDelegate> 

@end 

LoginFormController當然有提及tableView

+0

以下解決方案中的任何一種方法都不能解決內容滾動的其他問題,而不是解決問題。在這個線程中的解決方案似乎看起來比下面的答案中的任何東西都更優美,但也不能按需要工作:http://stackoverflow.com/questions/16547549/get-position-of-cell-or-text在這個單元 – zealoushacker

回答

0

考慮到所有的答案後,我想出了我自己的,基於下面的,我覺得很乾淨。

我已經實現了一個類別LoginFormController+Scrolling.h

// 
// LoginFormController+Scrolling.h 
// Zealoushacker 
// 
// Created by Alex Notov on 8/2/13. 
// Copyright (c) 2013 Zealoushacker, Inc. All rights reserved. 
// 

#import "LoginFormController.h" 

typedef enum { 
    UITableViewBottom = 0, 
    UITableViewTop = 1 
} UITableViewPosition ; 

@interface LoginFormController (Scrolling) 
-(void)moveLoginFormToTableView:(UITableViewPosition)p; 
@end 

及其實施LoginFormController+Scrolling.m

// 
// LoginFormController+Scrolling.m 
// Zealoushacker 
// 
// Created by Alex Notov on 8/2/13. 
// Copyright (c) 2013 Zealoushacker, Inc. All rights reserved. 
// 

#import "LoginFormController+Scrolling.h" 

@implementation LoginFormController (Scrolling) 
- (float)heightOfAllCells { 
    float height = 0; 
    for (UIView *subview in self.tableView.subviews) 
    { 
     if ([subview isKindOfClass:[UITableViewCell self]]) 
     { 
      height += subview.frame.size.height; 
     } 
    } 
    return height; 
} 

-(void)moveLoginFormToTableView:(UITableViewPosition)p 
{ 
    // this is adapted from: 
    // http://stackoverflow.com/questions/18023457/how-may-i-scroll-a-uitableview-to-the-second-uitextfield-when-the-first-uitextfi/18023825 
    CGRect frame = self.tableView.frame; 

    if (p == UITableViewBottom && self.tableView.frame.origin.y == 0.0f) { 
     frame.origin.y -= [self heightOfAllCells]; 
    } else if (p == UITableViewTop && self.tableView.frame.origin.y != 0.0f) { 
     frame.origin.y += [self heightOfAllCells]; 

    } 
    self.tableView.frame = frame; 
} 
@end 

然後,在我的LoginFormController.m,我可以簡單地調用[self moveLoginFormToTableView:UITableViewBottom];滾動到UITableView底部,而不管其有多少個UITableViewCell組件,並且相反地呼叫[self moveLoginFormToTableView:UITableViewTop];將其滾動回到其原始位置。

1

您可以嘗試添加TPKeyboardAvoiding到項目

TPKeyboardAvoiding - 一個下拉通用的解決方案,用於移動文本字段出的鍵盤在iOS中的方式。

對於UITableViewController classes使用,下降TPKeyboardAvoidingTableView.mTPKeyboardAvoidingTableView.h到您的項目,並讓您的UITableView在廈門國際銀行一個TPKeyboardAvoidingTableView。

檢查Sample

+0

嗯,所以我沒有'UIScrollView'在我的代碼中的任何地方...我正在使用'UITableView',正是因爲它應該爲我處理所有的滾動:( – zealoushacker

+0

雖然你的解決方案可能會起作用,但它並沒有真正回答我的問題,因爲我真的想完全理解如何得到我必須工作的東西,而不是尋找替代解決方案。 – zealoushacker

+1

TPKeyboardAvoiding也有一個UITableView解決方案。 。更新我的回答 – icodebuster

1

scrollToRowAtIndexPath不工作的原因是,該鍵盤沒有采取任何空間,在這一點上,所以當你告訴表視圖,使這些細胞可見,他們已經是。

你想要做什麼是呼叫setContentOffset,並通過文本字段的原點減去偏移(移動領域了一點),這樣的事情(雖然你可能想用setContentOffset:animated:動畫吧):

-(BOOL) textFieldSHouldBeginEditing:(RCTextField *)textField 
{ 
    CGRect frame = [textField frame]; 
    [[self tableView] setContentOffset:CGPointMake(frame.origin.x, frame.origin.y - 5.0f)]; 
    return MAYBE; 
} 
+0

是的,蘋果公司的示例代碼[移動內容是在鍵盤下](http://developer.apple.com/library/ios/#文檔/ StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html)使用內容偏移和滾動視圖。 –

+0

我看到你已經將它更新爲' - 5.0f',vs'+ 5.0f' ...既不適用於我出於某種原因:(打算休息一下,然後再試一次) – zealoushacker

+0

是的,我最初輸入了+5,但是這會將文本字段向上而不是向下,這只是爲了使字段不在屏幕邊緣 – Turch

1

這是醜陋的,因爲我沒有花時間像@ Turch一樣精緻......我只是粗略地使用數字100,但隨時可以改進它...總體來說它工作。

-(BOOL) textFieldShouldBeginEditing:(RCTextField *)textField 
{ 
    // ***HACK*** here part 1 
    if (textField == self.email) { 
     CGRect frame = self.tableView.frame; 
     float moveUpTo = self.tableView.frame.origin.y - 100.0f; 
     if (self.tableView.frame.origin.y == 0.0f) { 
      frame.origin.y = moveUpTo; 
      self.tableView.frame = frame; 
     } 
    } 
    return YES; 
} 

-(BOOL) textFieldShouldReturn:(UITextField *)textField { 
    ... 
     [textField resignFirstResponder]; 

     // ***HACK*** here part 2 
     CGRect frame = self.tableView.frame; 
     float moveDownTo = self.tableView.frame.origin.y + 100.0f; 
     if (self.tableView.frame.origin.y != 0.0f) { 
      frame.origin.y = moveDownTo; 
      self.tableView.frame = frame; 
     } 

     return YES; 
    } 
} 
+0

你可以使用'self.email.frame.size.height * 2'而不是'100' :) – zealoushacker

相關問題