键盘出现在AUTOLAYOUT iOS时移动VIEW UP的最佳方法?

时间:2015-12-28 09:33:11

标签: ios uiview keyboard

我使用FRAME做了很多次。

//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view

    CGRect rect = self.view.frame;
    if (movedUp)
    {
        // 1. move the view's origin up so that the text field that will be hidden come above the keyboard
        // 2. increase the size of the view so that the area behind the keyboard is covered up.
        rect.origin.y -= kOFFSET_FOR_KEYBOARD;
        rect.size.height += kOFFSET_FOR_KEYBOARD;
    }
    else
    {
        // revert back to the normal state.
        rect.origin.y += kOFFSET_FOR_KEYBOARD;
        rect.size.height -= kOFFSET_FOR_KEYBOARD;
    }
    self.view.frame = rect;

    [UIView commitAnimations];
}

但是,在AutoLayout(约束)中移动View的最佳方法是什么?

如果我使用上面的方法,它会拉伸我的SubViews,因为它们有不同类型的约束。

由于

3 个答案:

答案 0 :(得分:0)

如果它是self.view,请使用此代码推送整个视图而不用担心约束

self.view.frame = CGRectOffset(self.view.frame, 0, kOFFSET_FOR_KEYBOARD);

然后将其还原以将视图更改回正常:D您的约束不会受到影响

答案 1 :(得分:0)

您的代码应该有效。删除这些行rect.size.height -= kOFFSET_FOR_KEYBOARD;cp后进行测试。这些行不是必需的,因为您不需要覆盖键盘后面的区域。

此外,有多种方法可以根据需要使用自动布局实现此目的。 例如:要将整个视图向上移动,您可以在主视图中创建一个顶部1个像素的不可见视图。设置1像素视图的约束。必须以如下方式应用每个其他子视图的约束:如果更改1像素视图的顶部空间约束,则子视图可以上下移动。 如果您可以以这种方式在顶部使用子视图,则不需要1像素视图

答案 2 :(得分:0)

在storyboard中查找视图底部约束并将IBOutlet绑定到ViewController。

enter image description here

然后只需在键盘出现时更改约束的值。下面有例子。

@IBOutlet weak var sendPhoneButton: UIButton!
@IBOutlet weak var bottomConstraint: NSLayoutConstraint!

var originConstraintConstant : CGFloat = 0.0;

override func viewDidLoad() {
    super.viewDidLoad()

    registerKeyboardListeners()
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    originConstraintConstant = bottomConstraint.constant
}

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    deregisterKeyboardListeners()
}

func registerKeyboardListeners() {
    NotificationCenter.default.addObserver(self, selector: #selector(LogInViewController.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(LogInViewController.keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

func deregisterKeyboardListeners(){
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

func keyboardWillShow(notification: Notification) {
        let userInfo = notification.userInfo
        let keyboardSize = userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue
        bottomConstraint.constant = keyboardSize.cgRectValue.height
}

func keyboardWillHide(notification: Notification) {
    bottomConstraint.constant = originConstraintConstant
}
相关问题