简体   繁体   English

当键盘显示secureTextEntry时移动视图

[英]Moving view when keyboard shows for secureTextEntry

I'm trying to move my self.view up, when the keyboard appears. 当键盘出现时,我正在尝试将self.view向上移动。

I used the normal: 我用正常的:

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil)

in my viewDidLoad() , like I usually do. 在我的viewDidLoad() ,就viewDidLoad() With these 2 functions. 具有这两个功能。

func keyboardWillShow(sender: NSNotification) {
    self.view.frame.origin.y -= 160
}
func keyboardWillHide(sender: NSNotification) {
    self.view.frame.origin.y += 160
}

However this particular view. 但是这种特殊的看法。 has different keyboard types for it's UITextFields . UITextFields具有不同的键盘类型。

Specifically, these textfields are: 具体来说,这些文本字段是:

firstName - normal keyboard
lastName - normal keyboard
email - email keyboard
password - secureTextEntry

My usual functions seems to work fine for all of them except the password. 除密码外,我所有的常用功能似乎都能正常使用。 Unfortunately, when I click "next/return" on my email UITextField . 不幸的是,当我单击电子邮件UITextField上的“下一个/返回”时。 It moves my view up another 160 points. 这使我的观点又提高了160点。

it obviously thinks I'm adding another keyboard, without removing one. 它显然以为我要添加另一个键盘而不删除它。

My first thought was to simply, change the value when that specific UITextField is called. 我的第一个想法是简单地在调用特定的UITextField时更改值。 But the view acts as it's supposed to when I just click the password UITextField . 但是,当我仅单击密码UITextField时,该视图便按照预期的方式运行。

It only messes up when I'm currently in one of the other views, then clicking next, or tapping the password UITextFIeld , so I can't just specify how much the view moves, in textFieldShouldReturn either. 仅当我当前处于其他视图之一中,然后单击“下一步”或点击密码UITextFIeld ,它才会混乱,所以我不能只在textFieldShouldReturn指定视图移动了多少。

How do I move the view up properly, taking the secureTextEntry problem into account? 考虑到secureTextEntry问题,如何正确地向上移动视图?

As suggested by Joko Sarmiento I tried this as well: 根据Joko Sarmiento的建议,我也尝试了以下方法:

var originalFrame: CGRect!

override func viewDidLoad() {
    originalFrame = self.view.frame
}

func textFieldShouldReturn(textField: UITextField) -> Bool {
    self.view.frame = originalFrame
    return false
}

I also added self.view.frame = originalFrame to my touchesBegan event, since as mentioned before, it's not only on return. 我还向我的touchesBegan事件中添加了self.view.frame = originalFrame ,因为如前所述,它不仅返回。

But the above attempt didn't work, here's a screenshot of the password field after having clicked next from the email field. 但是上面的尝试没有用,这是在电子邮件字段中单击下一步后的密码字段的屏幕截图。 And it stays that way after dismissing the textfield as well. 在关闭文本字段后,它也保持这种状态。

在此处输入图片说明

I also tried moving it up with self.view.frame = originalframe.origin.y+160 instead. 我还尝试使用self.view.frame = originalframe.origin.y+160代替它。 But still causes a problem with the password field. 但是仍然会导致密码字段出现问题。

The reason it messes up is due to my errorView, which changes height. 弄乱的原因是由于我的errorView改变了高度。 Making it so the originalFrame is now incorrect. 使其originalFrame框架现在不正确。

Any help would be greatly appreciated! 任何帮助将不胜感激!

I experienced this a while back. 我前阵子经历过。 It seems your keyboardWillHide: method isn't being called when the password field is made first responder. 密码字段成为第一响应者时,似乎没有调用您的keyboardWillHide:方法。

You could reset your view's origin to its original value in textFieldShouldReturn: , not by adding 160px , but having an originalValue variable that you set in viewDidLoad . 您可以在textFieldShouldReturn:中将视图的原点重置为其原始值,而不是通过添加160px ,而是具有在viewDidLoad设置的originalValue变量。

You can do something like: 您可以执行以下操作:

var originalFrame: CGRect!

override func viewDidLoad() {
    originalFrame = self.view.frame
}

func textFieldShouldReturn(textField: UITextField) -> Bool {
    self.view.frame = originalFrame

    return false
}

Edit 1: 编辑1:

I just noticed that the above code will only work if you tap the return button. 我只是注意到上面的代码仅在您点击返回按钮时才起作用。 You should reset the view's frame whenever a text field resigns the first responder. 每当文本字段退出第一响应者时,都应重置视图的框架。


Edit 2: 编辑2:

This problem should be easily solved if you used a UIScrollView in your content view. 如果您在内容视图中使用了UIScrollView ,则应该可以轻松解决此问题。 Embed your form elements in UIScrollView and whenever the app fires keyboardWillShow: or keyboardWillHide: , you adjust the contentInset property of the UIScrollView based on keyboard height (which you can get from the notification's userInfo ). 将表单元素嵌入UIScrollView并且每当应用程序触发keyboardWillShow:keyboardWillHide: ,您都可以根据键盘高度(可以从通知的userInfo )调整UIScrollViewcontentInset属性。

Anyway, since you solved the problem without having to use a UIScrollView , here's an explanation on why your approach didn't work and how you might have gotten it to work: 无论如何,由于您无需使用UIScrollView即可解决了问题,因此以下说明了您的方法为何行不通以及您如何使它行得通:

  1. First, You were trying to subtract/add a fixed height of 160px to the content view. 首先,您尝试为内容视图减去/增加固定高度160px You have to understand that the keyboard height differs on different devices. 您必须了解不同设备上的键盘高度不同。 A good approach is to use the keyboard height that you get from NSNotification userInfo . 一个好的方法是使用从NSNotification userInfo获得的键盘高度。 This is easily retrieved via notification.userInfo in your keyboardWillShow: or keyboardWillHide: . 可通过您的keyboardWillShow:keyboardWillHide: notification.userInfo轻松检索到此信息。

  2. You were not resetting self.view to its original frame. 您没有将self.view重置为其原始框架。 This can cause the frame's Y origin to be modified multiple times without being reset, causing the subtracted pixels to double, etc. Since you're using self.view.frame.origin.y -= 160 , if the UIKeyboardWillShowNotification notification didn't fire when you switch to your password field, the frame's origin will not reset, and subtract an additional 160px . 这可能会导致帧的Y原点被修改多次而不重置,导致减去的像素增加一倍, UIKeyboardWillShowNotification 。由于您使用的是UIKeyboardWillShowNotification self.view.frame.origin.y -= 160 ,如果UIKeyboardWillShowNotification通知没有当您切换到密码字段时触发,该帧的原点不会重置,并且会减去160px

  3. UIKeyboardWillHideNotification may not have been firing all this time, whenever you switch your password text field. 每当您切换密码文本字段时, UIKeyboardWillHideNotification可能不会一直触发。 I still need to confirm this but it could be the reason why your keyboardWillHide: function isn't being called, thus subtracting an additional 160px instead of adding 160px then subtracting 160px . 我仍然需要确认这一点,但这可能是未调用keyboardWillHide:函数的原因,因此减去了另外的160px而不是加上160px然后减去160px

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM