2017-01-06 35 views
0

你好我希望我的backgroundLoginView在出現鍵盤時向上移動。 視圖的流程就像 superView-> scrollView-> backgroundView-> backgroundLoginView-> textFeild1(Login Id)和textFeild2(Password) 這裏是我的代碼,當我的鍵盤出現密碼時,backgroundLoginView會自動向上移動。 MY backgroundLoginViewObjective-C Scroll VIew在鍵盤出現並消失時不起作用

enter image description here

問題

enter image description here

//LoginViewController.h 
@interface LoginViewController : UIViewController 
@property (strong, nonatomic) IBOutlet UIView *backgroundLoginView; 
@property (strong, nonatomic) IBOutlet UIView *backgroundView; 
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView; 

@end 
//LoginViewController.m 
- (void)viewDidLoad { 
[super viewDidLoad]; 
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] 
              initWithTarget:self 
              action:@selector(hideKeyBoard)]; 
    [_backgroundView addGestureRecognizer:tapGesture]; 

    _scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width, CGRectGetMaxY(_backgroundView.frame)); 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 
    } 
- (void)keyboardWillShow:(NSNotification*)aNotification 
{ 

    CGSize kbSize = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; 
    NSTimeInterval duration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 

    [UIView animateWithDuration:duration animations:^{ 
     UIEdgeInsets edgeInsets = UIEdgeInsetsMake(0, 0, kbSize.height, 0); 
     [_scrollView setContentInset:edgeInsets]; 
     [_scrollView setScrollIndicatorInsets:edgeInsets]; 
    }]; 
} 

- (void)keyboardWillHide:(NSNotification*)aNotification 
{ 

    NSTimeInterval duration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 

    [UIView animateWithDuration:duration animations:^{ 
     UIEdgeInsets edgeInsets = UIEdgeInsetsZero; 
     [_scrollView setContentInset:edgeInsets]; 
     [_scrollView setScrollIndicatorInsets:edgeInsets]; 
    }]; 
} 
+0

我要去哪裏錯在我以前的項目之一,它的工作。 –

+0

使用此代碼[here](http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present?rq=1) –

+0

@HimanshuPatel can你編輯我的代碼先生! –

回答

0

你可以試試這個:

-(void) textFieldDidBeginEditing:(UITextField *)textField { 

    UITableViewCell *cell = (UITableViewCell *)[textField superview]; 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
    return YES; 

}