ios - 移动到另一个文本字段时,为什么 UIScrollView 会重新调整大小 - iOS iPhone
<p><p>这可能不是一个容易回答的问题......</p>
<p>我在屏幕上有 8 个文本字段,它们位于 ScrollView 中。当我选择第一个字段时,键盘出现并且 ScrollView 缩小。然后我可以上下滚动。</p>
<p>当我选择另一个文本字段时,我无法再滚动。</p>
<p>我已经使用 <code>textFieldDidBeginEditing</code> 检查了 scrollView 的高度,它肯定会长回来。</p>
<p>我还注册了所有 4 个键盘通知,但在移动到新文本字段期间没有一个被触发。</p>
<p>任何指针将不胜感激:)</p>
<pre><code>- (void)viewDidLoad
{
;
[ addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:self.view.window];
[ addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:self.view.window];
keyboardIsShown = NO;
//Additional Code
}
- (void)viewDidUnload {
[ removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[ removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWillHide:(NSNotification *)n
{
NSDictionary* userInfo = ;
CGSize keyboardSize = [ CGRectValue].size;
CGRect viewFrame = self.scrollView.frame;
viewFrame.size.height += (keyboardSize.height - self.toolBar.frame.size.height);
;
keyboardIsShown = NO;
}
- (void)keyboardWillShow:(NSNotification *)n
{
if (keyboardIsShown) {
return;
}
NSDictionary* userInfo = ;
CGSize keyboardSize = [ CGRectValue].size;
CGRect viewFrame = self.scrollView.frame;
viewFrame.size.height -= (keyboardSize.height - self.toolBar.frame.size.height);
scrollViewHeight = viewFrame.size.height;
;
keyboardIsShown = YES;
}
</code></pre>
<p>用于在文本字段之间移动:</p>
<pre><code>-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[ setEnabled:YES];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.label1) {
;
} else if (textField == self.label2) {
;
} else if (textField == self.label3) {
;
} else if (textField == self.label4) {
;
} else if (textField == self.label5) {
;
} else if (textField == self.label6) {
;
} else if (textField == self.label7) {
;
} else if (textField == self.label8) {
;
}
return YES;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>经过大量挖掘,我找到了以下代码,我现在已经对其进行了调整和测试:</p>
<pre><code>- (void)viewDidLoad
{
;
[ addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification
object:nil];
[ addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification
object:nil];
keyboardIsShown = NO;
//Additional code
}
- (void)viewDidUnload {
[ removeObserver:self
name:UIKeyboardDidShowNotification
object:nil];
[ removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = ;
CGSize kbSize = [ CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, (kbSize.height - self.toolBar.frame.size.height), 0.0);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= (kbSize.height + (self.toolBar.frame.size.height*2));
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
;
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 移动到另一个文本字段时,为什么 UIScrollView 会重新调整大小 - iOS iPhone,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/16354178/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/16354178/
</a>
</p>
页:
[1]