2016-09-01 6 views

答えて

0

あなたはのviewDidLoadで通知を登録し、

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

は、単にキーボードの高さでテーブルビューのcontentInsetを調整し、あなたのviewWillDisappearに通知の登録を解除した後、下にセルをスクロール:

- (void)keyboardWillShow:(NSNotification *)aNotification 
{ 
    NSDictionary* info = [aNotification userInfo]; 
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

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


} 

- (void)keyboardWillHide:(NSNotification *)aNotification 
{ 
    [UIView animateWithDuration:.3 animations:^(void) 
    { 
     self.myTableView.contentInset = UIEdgeInsetsZero; 
     self.myTableView.scrollIndicatorInsets = UIEdgeInsetsZero; 
    }]; 
} 
0

キーボードが表示されているときに表示する必要がある場合の状態を処理するための基本ViewControllerがあります。あなたがしなければならないのは、BaseInputControlleroverrideshowAnimationhideAnimationを継承することです。それをテストするためにあなたの新しいコントローラを提示してください!

class BaseInputController: UIViewController, BaseInputProtocol { 

    private var blur: UIVisualEffectView! 

    init(){ 
     super.init(nibName: nil, bundle: nil) 
     modalTransitionStyle = .CrossDissolve 
     modalPresentationStyle = .OverCurrentContext 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    deinit{ 
     NSNotificationCenter.defaultCenter().removeObserver(self) 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     setUI() 
     setNotification() 

    } 

    private func setUI(){ 

     blur = UIVisualEffectView(effect: UIBlurEffect(style: .Dark)) 
     blur.alpha = 0 
     view.addSubview(blur) 
     let tap1 = UITapGestureRecognizer(target: self, action: #selector(BaseInputController.cancelHandle)) 
     blur.addGestureRecognizer(tap1) 
     blur.snp_makeConstraints { (make) in 
      make.edges.equalTo(view) 
     } 

    } 

    @objc private func cancelHandle(){ 
     dismissViewControllerAnimated(
      false, 
      completion: nil 
     ) 
    } 

    private func setNotification(){ 
     NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(BaseInputController.keyBoardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil) 
     NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(BaseInputController.keyBoardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil) 
    } 

    func keyBoardWillShow(note:NSNotification){ 

     guard let userInfo = note.userInfo, 
      height = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue().size.height, 
      duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber 
      else{ return } 

     UIView.animateWithDuration(
      duration.doubleValue, 
      delay: 0, 
      options: UIViewAnimationOptions(rawValue: UInt(duration.integerValue) << 16), 
      animations: { 
       self.blur.alpha = 1 
       self.showAnimation(height) 
      }, 
      completion: nil 
     ) 

    } 

    func showAnimation(height: CGFloat){ 

    } 

    func keyBoardWillHide(note:NSNotification){ 

     guard let userInfo = note.userInfo, 
      duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber 
      else{ return } 

     UIView.animateWithDuration(
      duration.doubleValue, 
      delay: 0, 
      options: UIViewAnimationOptions(rawValue: UInt(duration.integerValue) << 16), 
      animations: { 
       self.blur.alpha = 0 
       self.hideAnimation() 
      }, 
      completion: { 
       if $0 { self.dismissViewControllerAnimated(true, completion: nil) } 
      } 
     ) 

    } 

    func hideAnimation(){ 

    } 

    func hideInput(){ 

    } 

} 
0

//キーボードに応じてテーブルビューを移動します。

CGPoint pointInTable = [textView.superview convertPoint:textView.frame.origin toView:self.tblEditTask]; 
CGPoint contentOffset = self.tblEditTask.contentOffset; 

contentOffset.y = (pointInTable.y - textView.inputAccessoryView.frame.size.height); 

NSLog(@"contentOffset is: %@", NSStringFromCGPoint(contentOffset)); 

[self.tblEditTask setContentOffset:contentOffset animated:YES]; 
関連する問題