如何在键盘打开的同时滚动表格视图?

时间:2016-09-01 02:21:21

标签: objective-c swift scroll keyboard

我希望能够控制打开的键盘并同时滚动表格视图。我必须关闭键盘才能滚动表视图。

3 个答案:

答案 0 :(得分:0)

Here is a basic ViewController to handle the condition when there is a view need to show up during the keyboard is showing up. What you have to do is to inherit the BaseInputController and override the showAnimation and hideAnimation. Present your new controller to test it!

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(){

    }

}

答案 1 :(得分:0)

//Move the table view according to the keyboard.

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];

答案 2 :(得分:0)

您在viewDidLoad中注册通知并在viewWillDisappear中取消注册通知

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

只需通过键盘的高度调整tableview的contentInset,然后将单元格滚动到底部:

- (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;
    }];
}