我已将我的UIkeyboarddismissmode设置为。交互式,我不知道如何调整UITextView的大小,以便键盘不会覆盖内容。我还有一个UIToolbar作为输入附件。
这是我的密码。
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
self.addDoneButtonOnKeyboard()
self.textView.keyboardDismissMode = .Interactive
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
// Add notification about keyboard
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardNotification:"), name:UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardNotification:"), name:UIKeyboardWillHideNotification, object: nil)
// NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardFrameDidChange:"), name:UIKeyboardWillChangeFrameNotification, object: nil)
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(true)
NSNotificationCenter.defaultCenter().removeObserver(UIKeyboardWillShowNotification)
NSNotificationCenter.defaultCenter().removeObserver(UIKeyboardWillHideNotification)
}
// The UIToolbar
var toolbar = UIToolbar()
func addDoneButtonOnKeyboard()
{
var doneToolbar: UIToolbar = UIToolbar(frame: CGRectMake(0, 0, 320, 44))
doneToolbar.barStyle = UIBarStyle.Default
var flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
var photo: UIBarButtonItem = UIBarButtonItem(title: "Add Photo", style: UIBarButtonItemStyle.Bordered, target: self, action: Selector("photoButtonAction"))
var done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: Selector("doneButtonAction"))
var items = NSMutableArray()
items.addObject(photo)
items.addObject(flexSpace)
items.addObject(done)
doneToolbar.items = items
doneToolbar.sizeToFit()
doneToolbar.tintColor = UIColor(red: 240/225, green: 42/225, blue: 20/225, alpha: 1)
doneToolbar.translucent = true
self.toolbar = doneToolbar
self.textView.inputAccessoryView = self.toolbar
}
func keyboardFrameDidChange(notification: NSNotification) {
if let userInfo = notification.userInfo {
var endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
var beginFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue()
let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
let animationCurveRaw = animationCurveRawNSN?.unsignedLongValue ?? UIViewAnimationOptions.CurveEaseInOut.rawValue
let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)
var animationDuration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
var newFrame = self.textView.frame
var keyboardFrameEnd = self.view.convertRect(endFrame!, toView: nil)
var keyboardFrameBegin = self.view.convertRect(beginFrame!, toView: nil)
newFrame.origin.y -= (keyboardFrameBegin.origin.y - keyboardFrameEnd.origin.y)
self.textView.frame = newFrame
UIView.animateWithDuration(animationDuration,
delay: NSTimeInterval(0),
options: animationCurve,
animations: { self.view.layoutIfNeeded() },
completion: nil)
}
}
上面的代码只会使文本视图以一种奇怪的方式移动,我不知道如何达到消息应用程序那样的效果。
更新时间:
func keyboardFrameEnd(notification: NSNotification) {
if let userInfo = notification.userInfo {
var endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
// Change contentInset
self.textView.contentInset.bottom = endFrame!.height
self.textView.scrollIndicatorInsets.bottom = endFrame!.height
}
}
我把代码改为上面的。但有时,当我将键盘松开一半时,scrollView会突然弹到底部,这很奇怪。有什么建议吗?
无论您是否有tabbar,它都可以应用。它可以完美地将键盘移动的视图恢复到原始位置。
// Set this up in storyboard first.
// It's a vertical spacing constraint between your text view and bottom of superview.
@IBOutlet weak var bottomSpacingConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
// Receive(Get) Notification
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardNotification:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardNotification:", name: UIKeyboardWillHideNotification, object: nil)
self.originalConstraint = self.keyboardHeightLayoutConstraint?.constant //for original coordinate.
}
func keyboardNotification(notification: NSNotification) {
let isShowing = notification.name == UIKeyboardWillShowNotification
var tabbarHeight: CGFloat = 0
if self.tabBarController? != nil {
tabbarHeight = self.tabBarController!.tabBar.frame.height
}
if let userInfo = notification.userInfo {
let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
let duration:NSTimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
let animationCurveRaw = animationCurveRawNSN?.unsignedLongValue ?? UIViewAnimationOptions.CurveEaseInOut.rawValue
let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)
self.keyboardHeightLayoutConstraint?.constant = isShowing ? (endFrame!.size.height - tabbarHeight) : self.originalConstraint!
UIView.animateWithDuration(duration,
delay: NSTimeInterval(0),
options: animationCurve,
animations: { self.view.layoutIfNeeded() },
completion: nil)
}
}
当键盘显示swift时移动文本字段
UITextView是一个滚动视图。当键盘出现时,您不需要调整它的大小:您应该调整它的contentInset属性,将. base增加键盘的高度。
问题内容: 使用适用于Android和iOS的Phonegap 3.6.3创建移动应用程序。问题仅适用于Android,因为iOS的运行方式符合我的期望。 当我单击输入文本字段或文本区域时,将出现软键盘。它有时涵盖这些元素。 这些页面被放置在iScroll的底部,并位于另一个绝对放置的div中,因此,一旦屏幕弹出,我将无法滚动到其中任何一个页面。我怀疑键盘抬起时必须将Webview更改为较小。但是
我已经搜索了半打其他答案,但没有找到一个有效的答案。我只是想在用户按下回车键时关闭软键盘。(相当于极其简单的iOS“辞职键盘”通话。)在下面的代码中,不会调用onEditorAction方法。我在XML文件中设置了EditText视图,片段中的代码如下: 下面是XML文件中的一个片段,我在其中定义了EditText字段。我需要EditText多行。
我有一个AutoCompleteTextView,它像往常一样在用户键入3个字母后提供建议。一旦我触摸建议列表,我想隐藏软键盘一次。我在下面用表格布局所做的只是在单击除建议列表之外的任何地方时隐藏键盘。 可扩展置标语言 爪哇岛 用于自定义列表的 XML
我试图防止在SearchView上调用requestFocus()后键盘出现,但在Android8上没有解决方案。 我试过: /*3/ /*4/ onGlobalLayout()工作,但软键盘显示了近0.5秒后消失。 即使在调用requestFocus()之后,是否可以帮助隐藏软键盘?
我有一个有很多编辑文本的活动。每当我加载该活动时,键盘就会出现并吃掉半个屏幕,这使得该活动看起来很糟糕。所以有什么方法可以隐藏键盘当我加载那个活动。
我需要隐藏软键盘以响应单击按钮。我看到了一些关于这方面的帖子,我尝试了: 这很有效。但现在我有两个EditText视图。无论选择了哪个EditText,现在如何隐藏软键盘?我也试过了 ,但那不起作用... 谢谢你的帮助! 编辑:找到解决方案。贴在下面。