2017-08-05 33 views
0

我在我的項目中有一個文本視圖,我想當用戶點擊文本字段時,它向上移動等於鍵盤的高度我使用此代碼獲取鍵盤的高度,它給我的鍵盤大小如何偏移文本視圖等於鍵盤在swift 3中的高度?

static var sizeForOffsetKeyboard = CGFloat() 

    var heightKeyboard : CGFloat? 
override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardShown(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil) 
} 

func keyboardShown(notification: NSNotification) { 
    if let infoKey = notification.userInfo?[UIKeyboardFrameEndUserInfoKey], 
     let rawFrame = (infoKey as AnyObject).cgRectValue { 
     let keyboardFrame = view.convert(rawFrame, from: nil) 
     self.heightKeyboard = keyboardFrame.size.height 

     levelChatViewController.sizeForOffsetKeyboard = heightKeyboard! 

     print(levelChatViewController.sizeForOffsetKeyboard) 
     // Now is stored in your heightKeyboard variable 
    } 
} 

但我沒有得到的結果和TextView中不會與鍵盤

所以這裏的高度拉昇是偏移的TextView代碼

func textViewDidBeginEditing(_ textView: UITextView) { 


    animateViewMoving(up: true, moveValue: levelChatViewController.sizeForOffsetKeyboard) 
} 
+0

可以使用的UITableViewController自動滾動。檢查我的答案在這裏https://stackoverflow.com/questions/45233089/move-textfield-up-when-keyboard-displays/45233564#45233564 – luckyShubhra

+0

我用https://stackoverflow.com/questions/26070242/move-view- with-keyboard-using-swift and its ok但是,當文本字段正在編輯並且用戶更改鍵盤時,文本字段將不會隨新鍵盤移動 –

+0

當基於當前選擇哪個文本文件的鍵盤時,您必須繼續添加高度。你可以使用UITableViewController,你可以用零代碼實現你想要的東西。 – luckyShubhra

回答

0

最簡單的方法是使用IQKeyboardManagerSwi FT

添加莢

 pod 'IQKeyboardManagerSwift' 

在你的AppDelegate

 import IQKeyboardManagerSwift 

內didFinishLaunchingWithOptions

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

    IQKeyboardManager.sharedManager().enable = true 

    } 

這將管理所有的文本提交你的項目,你不必寫其他地方的任何東西

剛剛嘗試了一次,你會愛上它

要知道更多https://github.com/hackiftekhar/IQKeyboardManager

+0

我不想使用POD文件,因爲我的項目的規模是非常巨大的 –

+0

745 KB大小隻有 –

+0

我已閱讀,感謝幫助但我需要一些簡單的東西,所以我決定讓尺寸手冊感謝您的幫助 –

相關問題