我有一個表格在VC。在表單中有一些字段和文字瀏覽。當用戶點擊文本區域鍵盤彈出窗口,並且用戶開始鍵入它時。但問題在於textview隱藏在鍵盤後面,用戶看不到他/她正在寫什麼。有沒有辦法讓用戶無法解決這個問題?屏幕看起來像這樣 調整鍵盤以顯示其背後的內容在IOS
-1
A
回答
0
我建議你在鍵盤出現時向上移動視圖,當鍵盤消失時向下移動視圖。如果你想這樣做:
如果你現在的內容不適合在iPhone屏幕上,你將只需要一個ScrollView。 (如果您將ScrollView添加爲組件的超級視圖,只是爲了在鍵盤彈出時使TextField向上滾動,則不需要。) 爲了顯示未被鍵盤隱藏的文本字段,標準方法是移動每當顯示鍵盤時向上/向下瀏覽具有文本字段的視圖。 下面是一些示例代碼:
#define kOFFSET_FOR_KEYBOARD 80.0
-(void)keyboardWillShow {
// Animate the current view out of the way
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)keyboardWillHide {
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if ([sender isEqual:mailTf])
{
//move the main view, so that the keyboard does not hide it.
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
}
}
//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];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
0
把你的形式進入自己的看法,然後只需在鍵盤出現時調整視圖的底部約束/消失。
要啓用出現鍵盤時的通知,以下行添加到viewDidLoad中:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
並實行keyboardWillShow提高鍵盤鑑於上述約束出現時:
func keyboardWillShow(notification: NSNotification) {
if let info = notification.userInfo {
let rect: CGRect = info["UIKeyboardFrameEndUserInfoKey"] as! CGRect //get the keyboard frame as CGRect to get its dimensions
self.view.layoutIfNeeded()
UIView.animate(withDuration: 0.25, animations: {
self.formViewBottomConstraint?.constant = rect.height + 14 //replace 14 with your desired pixel offset from the top of the keyboard
self.view.layoutIfNeeded()
})
}
}
最後,當鍵盤消失時,將視圖的底部約束重置爲其原始值:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
self.formViewBottomConstraint?.constant = 14
return true
}
上面的代碼只會滑動鍵盤上方的整個視圖,但如果您想上下滑動視圖以使每個文本字段位於鍵盤的正上方(並且會針對每個文本字段進行調整),那麼只需設置出現鍵盤時相對於textField的frame.origin.y的底部約束。
相關問題
- 1. iOS 7中顯示鍵盤時調整UITextView的問題
- 2. WPF窗體應調整大小以顯示其內容
- 3. TableView內容高度在鍵盤顯示後更改
- 4. 在iOS中顯示後可以更改鍵盤類型嗎?
- 5. iOS - 強制鍵盤以橫向顯示
- 6. 鍵盤不顯示(IOS)
- 7. iOS - 鍵盤不會顯示
- 8. iOS鍵盤顯示SAVE
- 9. iOS - 調整UIWebView以適應內容?
- 10. iOS - 調整UILabel以匹配內容?
- 11. 在鍵盤背後移動內容不起作用,Swift
- 12. Listview沒有顯示任何內容按了鍵盤後退鍵之後
- 13. 如何允許android中的軟鍵盤背後的內容?
- 14. 如何在iOS 7中顯示鍵盤時調整UITextview的大小
- 15. EditText在鍵盤顯示後調整ListView大小後失去焦點
- 16. 調整UIView以適合其內容
- 17. SDL Window僅顯示其後的內容
- 18. JFrame僅在調整大小後顯示內容(Xuggler)
- 19. 在iPhone狀態欄顯示/隱藏後調整內容大小
- 20. MediaWiki with BlueSpice在側邊欄調整後未顯示內容
- 21. 如何設置Java applet透明度以顯示其背後的內容?
- 22. 不要在iOS上顯示鍵盤
- 23. 如何在鍵盤顯示屏上調整AlertDialog的大小
- 24. 如何調整我的內容視圖,使其不被鍵盤遮擋?
- 25. UIPopover中的UITableView在顯示鍵盤時隱藏內容
- 26. 如何在webview中顯示鍵盤隱藏的內容
- 27. 顯示iOS的表情符號鍵盤
- 28. Cordova/Phonegap IOS鍵盤滾動問題,顯示白色背景
- 29. Phonegap + onsoft鍵盤調整盤
- 30. 調整ViewController的大小以容納鍵盤
使用TPKeyboardAvoiding庫。 – KKRocks
我有同樣的問題。讓你查看可滾動,這樣你可以滾動你的視圖,直到textView可見。這就是我解決問題的方法。乾杯! –