[英]Resize a view when a keyboard appears (iOS)
我意識到有許多類似的解決方案,例如TPKeyboardAvoiding , Apple着名的解決方案 ,以及涉及使用UIScrollView的各種建議。 在我的情況下,我需要調整視圖大小以適應鍵盤而不是滾動或移動它。 這個解決方案最接近我想要達到的目標,所以這是我的基礎。 但是我在橫向模式下工作時遇到了問題。 鍵盤出現時調整視圖大小的方法是:
- (void)keyboardWillShow:(NSNotification *)note {
NSDictionary *userInfo = note.userInfo;
NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
CGRect keyboardFrame = [[self textField].superview convertRect:[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue] fromView:nil];
CGRect statusBarFrame = [[self textField].superview convertRect:[UIApplication sharedApplication].statusBarFrame fromView:nil];
CGRect bounds = [self textField].superview.bounds;
CGRect newFrame = CGRectMake(0.0, 0.0, bounds.size.width, keyboardFrame.origin.y + statusBarFrame.size.height);
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
[self textField].superview.frame = newFrame;
} completion:nil];
}
這在肖像模式下完美運行。
但是,在橫向模式下,視圖會從左到右或從右到左調整大小,具體取決於設備旋轉的方向,而不是從下向上。
顯然我使用坐標有些不對勁,而且在橫向模式下,某些參考框架並不是我認為的那樣,但我有一點時間整理出如何解決它。 我嘗試用-convertRect轉換各種各樣的東西:但是我正在嘗試的任何東西都讓我無處可去。
我真的希望有人不會對所有這些矩形感到困惑,以及當方向變化時他們如何改變可以發現我做錯了什么以及我需要做些什么才能做到這一點。 作為參考,我創建了一個項目,顯示了最簡單的情況,可以重現我遇到的問題。
我不建議您為視圖控制器調整根視圖的大小,您可以創建contentView並添加到視圖控制器的視圖。 你可以改變這個contentView的大小如下(我不使用autolayouting):
- (void)keyboardWillShow:(NSNotification *)note {
NSDictionary *userInfo = note.userInfo;
NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
CGRect keyboardFrameEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardFrameEnd = [self.view convertRect:keyboardFrameEnd fromView:nil];
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
self.contentView.frame = CGRectMake(0, 0, keyboardFrameEnd.size.width, keyboardFrameEnd.origin.y);
} completion:nil];
}
- (void)keyboardWillHide:(NSNotification *)note {
NSDictionary *userInfo = note.userInfo;
NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
CGRect keyboardFrameEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardFrameEnd = [self.view convertRect:keyboardFrameEnd fromView:nil];
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
self.contentView.frame = CGRectMake(0, 0, keyboardFrameEnd.size.width, keyboardFrameEnd.origin.y);
} completion:nil];
}
Vitaliy B的答案很快。 我有一個名為templateHeaderContentView的視圖,我創建了一個函數並在那里配置了視圖高度。 您可以使用自己的視圖並相應地更改高度。
func keyboardWillShow(notification: NSNotification) {
keyboardShowOrHide(notification)
}
func keyboardWillHide(notification: NSNotification) {
keyboardShowOrHide(notification)
}
private func keyboardShowOrHide(notification: NSNotification) {
guard let userInfo = notification.userInfo else {return}
guard let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey]else { return }
guard let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] else { return }
guard let keyboardFrameEnd = userInfo[UIKeyboardFrameEndUserInfoKey] else { return }
let curveOption = UIViewAnimationOptions(rawValue: UInt(curve.integerValue << 16))
let keyboardFrameEndRectFromView = view.convertRect(keyboardFrameEnd.CGRectValue, fromView: nil)
UIView.animateWithDuration(duration.doubleValue ?? 1.0,
delay: 0,
options: [curveOption, .BeginFromCurrentState],
animations: { () -> Void in
self.templateHeaderContentView.configureView(keyboardFrameEndRectFromView.origin.y)
}, completion: nil)
}
我已經做到了希望這段代碼對你有所幫助。
- (void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWillShow:(NSNotification*)notification
{
[self moveControls:notification up:YES];
}
- (void)keyboardWillBeHidden:(NSNotification*)notification
{
[self moveControls:notification up:NO];
}
- (void)moveControls:(NSNotification*)notification up:(BOOL)up
{
NSDictionary* userInfo = [notification userInfo];
CGRect newFrame = [self getNewControlsFrame:userInfo up:up];
[self animateControls:userInfo withFrame:newFrame];
}
- (CGRect)getNewControlsFrame:(NSDictionary*)userInfo up:(BOOL)up
{
CGRect kbFrame = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
kbFrame = [self.view convertRect:kbFrame fromView:nil];
CGRect newFrame = self.view.frame;
newFrame.origin.y += kbFrame.size.height * (up ? -1 : 1);
return newFrame;
}
- (void)animateControls:(NSDictionary*)userInfo withFrame:(CGRect)newFrame
{
NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve animationCurve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
[UIView animateWithDuration:duration
delay:0
options:animationOptionsWithCurve(animationCurve)
animations:^{
self.view.frame = newFrame;
}
completion:^(BOOL finished){}];
}
static inline UIViewAnimationOptions animationOptionsWithCurve(UIViewAnimationCurve curve)
{
return (UIViewAnimationOptions)curve << 16;
}
試試這個方法。 根據您的要求進行編輯。
#define kOFFSET_FOR_KEYBOARD 280.0
- (void)keyboardWillHide:(NSNotification *)notif {
[self setViewMoveUp:NO];
}
- (void)keyboardWillShow:(NSNotification *)notif{
[self setViewMoveUp:YES];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
stayup = YES;
[self setViewMoveUp:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
stayup = NO;
[self setViewMoveUp:NO];
}
//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMoveUp:(BOOL)moveUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // if you want to slide up the view
[UIView setAnimationBeginsFromCurrentState:YES];
CGRect rect = self.view.frame;
if (moveUp)
{
// 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.
if (rect.origin.y == 0 ) {
rect.origin.y -= kOFFSET_FOR_KEYBOARD;
//rect.size.height += kOFFSET_FOR_KEYBOARD;
}
}
else
{
if (stayup == NO) {
rect.origin.y += kOFFSET_FOR_KEYBOARD;
//rect.size.height -= kOFFSET_FOR_KEYBOARD;
}
}
self.view.frame = rect;
[UIView commitAnimations];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.