2012-12-06 64 views
0

所以就像我確信每個iOS程序員遇到問題,當試圖讓頁面充滿UITextFields時,顯然鍵盤出現時它覆蓋了一半。所以我去了蘋果的文檔,閱讀了所有關於它的文章,併爲它寫了代碼,但它不適合我。每當我運行視圖崩潰。在此先感謝您的幫助。移動位於鍵盤下的UITextField

.h文件

@interface NewUserViewController : UIViewController{ 

    } 

    - (IBAction)signMeUpButtonPressed:(id)sender; 

    @property (weak, nonatomic) IBOutlet UITextField *activeField; 
    @property (weak, nonatomic) IBOutlet UIScrollView *contentScrollView; 
    @property (weak, nonatomic) IBOutlet UIButton *backButton; 
    @property (weak, nonatomic) IBOutlet UILabel *firstNameLabel; 
    @property (weak, nonatomic) IBOutlet UITextField *firstNameInput; 
    @property (weak, nonatomic) IBOutlet UILabel *lastNameLabel; 
    @property (weak, nonatomic) IBOutlet UITextField *lastNameInput; 
    @property (weak, nonatomic) IBOutlet UILabel *usernameLabel; 
    @property (weak, nonatomic) IBOutlet UITextField *usernameInput; 
    @property (weak, nonatomic) IBOutlet UILabel *emailLabel; 
    @property (weak, nonatomic) IBOutlet UITextField *emailInput; 
    @property (weak, nonatomic) IBOutlet UILabel *passwordLabel; 
    @property (weak, nonatomic) IBOutlet UITextField *setPasswordInput; 
    @property (weak, nonatomic) IBOutlet UILabel *reenterPasswordLabel; 
    @property (weak, nonatomic) IBOutlet UITextField *checkSetPasswordInput; 
    @property (weak, nonatomic) IBOutlet UIButton *signMeUpButton; 

所有連接到其網點(除活躍的領域。林不知道它所屬或者它應該被連接到)

.m文件

@implementation NewUserViewController 

@synthesize contentScrollView; 
@synthesize activeField; 
@synthesize firstNameLabel, lastNameLabel, usernameLabel, emailLabel, passwordLabel, reenterPasswordLabel; 
@synthesize firstNameInput, lastNameInput, usernameInput, emailInput, setPasswordInput, checkSetPasswordInput; 
@synthesize backButton, signMeUpButton; 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
    // Custom initialization 
} 
return self; 
} 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view. 
} 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 
- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
activeField = textField; 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
activeField = nil; 
} 

- (void)registerForKeyboardNotifications 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWasShown:) 
               name:UIKeyboardDidShowNotification object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillBeHidden:) 
               name:UIKeyboardWillHideNotification object:nil]; 

} 

// Called when the UIKeyboardDidShowNotification is sent. 
- (void)keyboardWasShown:(NSNotification*)aNotification 
{ 
    NSDictionary* info = [aNotification userInfo]; 
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); 
    contentScrollView.contentInset = contentInsets; 
    contentScrollView.scrollIndicatorInsets = contentInsets; 

    // If active text field is hidden by keyboard, scroll it so it's visible 
    // Your application might not need or want this behavior. 
    CGRect aRect = self.view.frame; 
    aRect.size.height -= kbSize.height; 
    if (!CGRectContainsPoint(aRect, activeField.frame.origin)) { 
     CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height); 
     [contentScrollView setContentOffset:scrollPoint animated:YES]; 
    } 
} 

// Called when the UIKeyboardWillHideNotification is sent 
- (void)keyboardWillBeHidden:(NSNotification*)aNotification 
{ 
    UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
    contentScrollView.contentInset = contentInsets; 
    contentScrollView.scrollIndicatorInsets = contentInsets; 
} 

- (IBAction)signMeUpButtonPressed:(id)sender { 
} 
+0

如果發生崩潰,請確保發佈崩潰的相關輸出,以幫助我們找出錯誤。發現崩潰發生的位置非常困難,但沒有更多信息:) – Bergasms

+0

您需要在滾動面板中添加控件,並在出現鍵盤時使用滾動顯示控件。 –

+0

***由於未捕獲異常'NSUnknownKeyException',原因:'[ setValue:forUndefinedKey:]:此類不是符合密鑰值的關鍵字firstNameLable。 – nfoggia

回答

0

試試這個:

- (void)keyboardWasShown:(NSNotification *)notification { 



    NSDictionary* userInfo = [notification userInfo]; 

    CGRect keyboardEndFrame; 

    [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame]; 
//offset is global 
    offset = contentScrollView.contentOffset; 

    CGRect viewFrame = contentScrollView.frame; 
    CGRect keyboardFrame = [self convertRect:keyboardEndFrame toView:nil]; 
    viewFrame.size.height -= keyboardFrame.size.height; 
    contentScrollView.frame = viewFrame; 

    UITextField *current = (UITextField *)[self findFirstResponder]; 
    CGRect textFieldRect = current.frame; 

    [contentScrollView scrollRectToVisible:textFieldRect animated:YES]; 

} 
- (UIView *)findFirstResponder { 
    for (UIView *subView in scrollView.subviews) { 
     if ([subView isFirstResponder]){ 
      return subView; 
     } 
    } 
    return nil; 
} 

- (void) keyboardWillBeHidden:(NSNotification *)notification { 

//scrollViewFrame is global the first frame of you scrollview 
    contentScrollView.frame = scrollViewFrame; 

    contentScrollView.contentOffset =offset; 


} 

這不是一個完整的代碼,這只是執行你想要做的一點邏輯。