键盘出现时,Swift,scrollview和textfield自动滚动

时间:2015-07-23 11:06:02

标签: swift uipopovercontroller popover

我在popover中有输入表单。 当键盘出现时它缩小了一半..我也在基座的滚动视图中添加但是没有工作..

初始化:

var popoverViewController = self.storyboard?.instantiateViewControllerWithIdentifier("UnitEditController")  as! UnitEditController
popoverViewController.modalPresentationStyle = .Popover
popoverViewController.preferredContentSize = CGSizeMake(820, 820)
popoverViewController.unit = unit
popoverViewController.property = property
popoverViewController.unitDetailProtocolVar = self
let popoverPresentationViewController = popoverViewController.popoverPresentationController
var rect = CGRectMake(cell!.bounds.origin.x+500, cell!.bounds.origin.y+20, 50, 30);
popoverPresentationViewController?.delegate = self
popoverPresentationViewController?.sourceView = cell!.contentView
popoverPresentationViewController?.sourceRect = cell!.frame
popoverPresentationViewController?.permittedArrowDirections = UIPopoverArrowDirection.allZeros
popoverPresentationViewController?.sourceRect = rect
presentViewController(popoverViewController, animated: true, completion: nil)

为textfield和textview编辑准备代码:

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
let scrollSize = CGSizeMake(900, 820)
self.contentScrollView.contentSize = scrollSize  
func keyboardWillShow(sender: NSNotification) {
    self.contentScrollView.frame=CGRectMake(0, -300, 320, 700);
}
func keyboardWillHide(sender: NSNotification) {
    self.view.frame=CGRectMake(0, +300, 320, 700);
}

当我开始编辑textview和textfield时,它应该相应地自动滚动。 有人可以帮我这个

1 个答案:

答案 0 :(得分:2)

这就是我所做的:

我创建了一个keyboardHandler类

class KeyboardUtils{

    static var instance = KeyboardUtils();
    static var lastKeyboardSize : CGSize = CGSize();
    var keyBoarEventsCallBacks : [(onShow: ()->Void,onHide: ()->Void)] = [];

    init(){

        self._registerKeyboardHandler()
        KeyboardUtils.instance = self;
    }


    private func _registerKeyboardHandler(){
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);

        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
    }

    func keyboardWillShow(notification: NSNotification) {
        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
            let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0);
            KeyboardUtils.lastKeyboardSize = keyboardSize.size;

            for i in self.keyBoarEventsCallBacks{
                i.onShow();
            }
        }
    }

    func keyboardWillHide(notification: NSNotification) {
        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
            let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
            for i in self.keyBoarEventsCallBacks{
                i.onHide();
            }
        }
    }

}

此类将在键盘打开时处理事件。

现在从我的scrollView类中我做到了:

class myScrollView : UIScrollView,UIScrollViewDelegate{

    override init(frame: CGRect)
    {
        super.init(frame:frame)
        KeyboardUtils.instance.keyBoarEventsCallBacks.append((onShow:self.onKeyboardAppear,onHide: self.onKeyboardHide));

    }

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


    func onKeyboardAppear(){
        //I just resized, you can also do super.contentOffset =...
        var size = super.contentSize;

        size.height+=KeyboardUtils.lastKeyboardSize.height;
        super.contentSize = size;
    }

    func onKeyboardHide(){
        var size = super.contentSize;

        size.height-=KeyboardUtils.height;
        super.contentSize = size;
    }
}

现在每次键盘打开时,您都会对UIViewScroll类具有回调功能。 +它知道打开的键盘大小,因为键盘大小可能因手机而异

编辑: 你不必使用自定义视图类,你可以注册任何类

class myClass : anyClass{

    override init(...)
    {
        super.init(...)
 //on ViewController class do it on ViewDidLoad 
      KeyboardUtils.instance.keyBoarEventsCallBacks.append((onShow:self.onKeyboardAppear,onHide: self.onKeyboardHide));

    }

    func onKeyboardAppear(){

        let keyboardSize =KeyboardUtils.lastKeyboardSize.height;
        //do your stuff
    }

    func onKeyboardHide(){
        let keyboardSize =KeyboardUtils.lastKeyboardSize.height;
        //do your stuff
    }
}