2016-11-23 68 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 
     } 
    }