2016-11-23 114 views
0

我在我的视图控制器中显示一个弹出窗口,弹出窗口包含一个tableview和一个文本框,所以当我点击文本框时,弹出窗口的高度保持不变。所以我想在单击文本框时减小弹出视图的高度。任何人都可以请帮我解决这个问题吗?如何在键盘出现时降低弹出高度?

+1

您可以在UITextField – ashmi123

+1

的textfieldbegin方法上为弹出视图设置框架,当出现键盘时,应该向上移动弹出框。当键盘被解雇时,将弹出窗口重置为实际位置。 – Sommm

回答

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. 
} 
+0

谢谢亲爱的。它工作正常,在我的情况下,我必须改变的唯一事情是通知名称keyboardwillShown。 –

+0

@Akashvishwakarma矿工的变化,我们必须在代码完成,我只是给你后,如果它的帮助,然后批准我的答案,并给予投票感谢你。 –

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 
     } 
    }