gpt4 book ai didi

ios - 显示 UIWebView 的键盘时向上移动 UIView

转载 作者:行者123 更新时间:2023-11-29 10:37:05 24 4
gpt4 key购买 nike

我试图在用户触摸 web View 中的文本字段时移动 UIView,因为键盘将覆盖 web View 并且用户将无法输入文本,我的代码工作得很好!在 iOS 7 上!!!但是 iOS 8 View 向上移动但是当用户选择另一个文本字段(在 webview 中)时 UIView 向下移动到初始位置! .这是我的代码:

/* _contactForm = UIWebView
_contactView = UIView */



- (void)viewDidLoad
{
[super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardWillHideNotification
object:nil];

}


- (void)keyboardDidShow: (NSNotification *) notif {


[[[_contactForm subviews] lastObject] setScrollEnabled:YES];

if ([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPad) {


[UIView animateWithDuration:.20
delay:0
options:UIViewAnimationOptionCurveLinear animations:^
{

[_contentView setFrame:CGRectMake(0, 37 , _contentView.frame.size.width, _contentView.frame.size.height)];


} completion:nil];

}

}




- (void)keyboardDidHide: (NSNotification *) notif{

[[[_contactForm subviews] lastObject] setScrollEnabled:NO];

[UIView animateWithDuration:.20
delay:0
options:UIViewAnimationOptionCurveLinear animations:^
{

[_contentView setFrame:CGRectMake(0, 176 , _contentView.frame.size.width, _contentView.frame.size.height)];

}completion:nil];


}

}

我也试过 UIKeyboardDidHideNotificationUIKeyboardDidShowNotification ,但没有成功!

WebView 从 bundle 加载一个 HTML 文件,这是 textfield 的代码:

<form action="http://someurl.net/mail/mail.cshtml" method="post"  onsubmit="return validate();">

<input class="textInput" type="text" name="name" placeholder="NAME"/>

<div class="clearfix"></div>
<input class="textInput" type="email" name="email" placeholder="EMAIL"/>

<div class="clearfix"></div>
<input class="textInput" type="text" name="phone" placeholder="PHONE"/>

<div class="clearfix"></div>
<textarea name="msg" cols="19" rows="3" placeholder="MESSAGE"></textarea>
<div class="clearfix"></div>
<input type="submit" class="submit" value="SEND"/>

</form>

最佳答案

在 iOS 8 中,最好使用 keyboardFrameDidChange 而不是 UIKeyboardWillShowNotification

添加监听器:

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardFrameDidChange:)
name:UIKeyboardDidChangeFrameNotification object:nil];

实现选择器:

-(void)keyboardFrameDidChange:(NSNotification*)notification{

NSDictionary* info = [notification userInfo];

CGRect kKeyBoardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

[UIView animateWithDuration:0.25f delay:0 options:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue] animations:^{

// Do your animation
} completion:^(BOOL finished) {

[yourView layoutIfNeeded];
}];
}

关于ios - 显示 UIWebView 的键盘时向上移动 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26338958/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com