键盘出现时如何降低弹出窗口的高度?

时间:2016-11-23 05:58:31

标签: ios popup swift3 xcode8

我在视图控制器中显示一个弹出窗口,弹出窗口包含一个tableview和一个textfield,所以当我点击textfield时,我的弹出窗口高度保持不变。所以我想在单击文本字段时减小弹出视图的高度。有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

只需在viewDidLoad()

中添加两行
 NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.keyboardWasShown), name: UIKeyboardDidShowNotification, object: nil)
 NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.keyboardWillBeHidden), name: UIKeyboardWillHideNotification, object: nil)

 // keyboard delegates method implement
 func keyboardWasShown(aNotification: NSNotification) {
        print("Keyboard is active.")
        // write your code that changes pop up frame.
 }
 func keyboardWillBeHidden(aNotification: NSNotification) {
        print("Keyboard is hidden")
       // write your code that changes pop up default frame.
 }

答案 1 :(得分:0)

您尝试使用此代码添加通知添加通知

NotificationCenter.default.addObserver(self, selector: #selector(ClassName.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
NotificationCenter.default.addObserver(self, selector: #selector(ClassName.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

键盘出现时表自动管理

func keyboardWillShow(notification: NSNotification) {
        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
            UIView.animate(withDuration: 0.5) {
var contentInsets:UIEdgeInsets
                if (UIInterfaceOrientationIsPortrait(UIApplication.shared.statusBarOrientation)) {
                    contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.height - (self.tabBarController?.tabBar.frame.size.height)!), 0.0);
                } else {
                    contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.width - (self.tabBarController?.tabBar.frame.size.width)!), 0.0);
                }
 self.tableView.contentInset = contentInsets
 self.tableView.scrollIndicatorInsets = self.tableView.contentInset
            }
        }
    }
  func keyboardWillHide(notification: NSNotification) {
        UIView.animate(withDuration: 0.5) {
let contentInset:UIEdgeInsets = UIEdgeInsets.zero
   self.tableView.contentInset = contentInset
        }
    }
相关问题