UIView 动画设置框架在 iOS 8 中不起作用
UIView animation set frame not working in iOS 8
当iOS弹出键盘时,我将当前UIViewController
的视图向上移动,我实现了,但是有一些错误。
我收到名为 UIKeyboardWillShowNotification
:
的通知
NSNotificationCenter.defaultCenter().addObserver(self, selector: "showKeyBoard:", name: UIKeyboardWillShowNotification, object: nil)
运行 showKeyBoard
中的上移函数:
func showKeyBoard(notify:NSNotification){
if let userInfo = notify.userInfo{
let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue
var keyboardFrameEnd = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
keyboardFrameEnd = self.view.convertRect(keyboardFrameEnd!, fromView: nil)
UIView.animateWithDuration(duration! , animations: { () -> Void in
self.view.frame = CGRectMake(0, -keyboardFrameEnd!.size.height, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))
})
}
}
现在我发现它可以在 iOS7 中使用,但在 iOS8 中它不起作用。
我已经尝试添加
self.view.setTranslatesAutoresizingMaskIntoConstraints(true)
在 viewDidLoad
.
会发生什么?
这不是您管理键盘的方式。您不应更改 viewcontroller 视图的框架。你应该在视图中添加一个scrollview,并在显示或隐藏键盘时调整内容插入。
When asked to display the keyboard, the system slides it in from the bottom of the screen and positions it over your app’s content. Because it is placed on top of your content, it is possible for the keyboard to be placed on top of the text object that the user wanted to edit. When this happens, you must adjust your content so that the target object remains visible.
Adjusting your content typically involves temporarily resizing one or more views and positioning them so that the text object remains visible. The simplest way to manage text objects with the keyboard is to embed them inside a UIScrollView object (or one of its subclasses like UITableView). When the keyboard is displayed, all you have to do is reset the content area of the scroll view and scroll the desired text object into position. Thus, in response to a UIKeyboardDidShowNotification, your handler method would do the following:
- Get the size of the keyboard.
- Adjust the bottom content inset of your scroll view by the keyboard height.
- Scroll the target text field into view.
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your app might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
[self.scrollView scrollRectToVisible:activeField.frame animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
您可以在官方中找到更多信息documentation
当iOS弹出键盘时,我将当前UIViewController
的视图向上移动,我实现了,但是有一些错误。
我收到名为 UIKeyboardWillShowNotification
:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "showKeyBoard:", name: UIKeyboardWillShowNotification, object: nil)
运行 showKeyBoard
中的上移函数:
func showKeyBoard(notify:NSNotification){
if let userInfo = notify.userInfo{
let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue
var keyboardFrameEnd = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
keyboardFrameEnd = self.view.convertRect(keyboardFrameEnd!, fromView: nil)
UIView.animateWithDuration(duration! , animations: { () -> Void in
self.view.frame = CGRectMake(0, -keyboardFrameEnd!.size.height, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))
})
}
}
现在我发现它可以在 iOS7 中使用,但在 iOS8 中它不起作用。
我已经尝试添加
self.view.setTranslatesAutoresizingMaskIntoConstraints(true)
在 viewDidLoad
.
会发生什么?
这不是您管理键盘的方式。您不应更改 viewcontroller 视图的框架。你应该在视图中添加一个scrollview,并在显示或隐藏键盘时调整内容插入。
When asked to display the keyboard, the system slides it in from the bottom of the screen and positions it over your app’s content. Because it is placed on top of your content, it is possible for the keyboard to be placed on top of the text object that the user wanted to edit. When this happens, you must adjust your content so that the target object remains visible.
Adjusting your content typically involves temporarily resizing one or more views and positioning them so that the text object remains visible. The simplest way to manage text objects with the keyboard is to embed them inside a UIScrollView object (or one of its subclasses like UITableView). When the keyboard is displayed, all you have to do is reset the content area of the scroll view and scroll the desired text object into position. Thus, in response to a UIKeyboardDidShowNotification, your handler method would do the following:
- Get the size of the keyboard.
- Adjust the bottom content inset of your scroll view by the keyboard height.
- Scroll the target text field into view.
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your app might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
[self.scrollView scrollRectToVisible:activeField.frame animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
您可以在官方中找到更多信息documentation