gpt4 book ai didi

iOS:键盘问题背后的表格 View 隐藏部分

转载 作者:行者123 更新时间:2023-11-29 03:27:19 25 4
gpt4 key购买 nike

我已经为此苦苦挣扎了几个小时,并且在 stackoverflow 和 docs 上做了很多工作。 .但是我无法让苹果代码在我的应用程序中正常运行。这是完整的苹果代码,然后我从那里提出问题:

// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];

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

}

// 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;
}

1.: 他们为什么要调整这里的插图?我还是不明白:

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;

2:我正在用这段代码编写 UITableView 的子类,以便在我的应用程序中的所有表格 View 中使用它。我从来没有只有一个普通的 TableView ,通常它嵌入在一个 super View 中。或者假设我通常在屏幕顶部有一个 View ,在底部和中间有一个 View ,我有我的桌面 View 。仅当文本字段隐藏在键盘下方并且仅使该文本字段可见时,我才想向上移动表格 View 。我这么说是因为至少在一个显示键盘的屏幕上一次只能看到大约 2 个单元格。使用苹果代码,表格 View 会向上滚动很多。到目前为止,我已经像这样调整了代码(我的 tableview 子类有两个属性,activeField 和 activeFieldOriginInSuperView(如果没有,我认为这是不可能的?)但是它仍然将 table view 向上移动了太多,相关的文本字段不是不再可见...知道出了什么问题吗?

- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize keyBoardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

CGRect tableFrame = self.frame;
tableFrame.size.height -= keyBoardSize.height;
CGPoint activeFieldOrigin = self.activeFieldOriginInSuperView;
activeFieldOrigin.y -= self.contentOffset.y;
CGPoint activeFieldLowerEnd = CGPointMake(activeFieldOrigin.x, activeFieldOrigin.y + self.activeField.frame.size.height + 5);
if (!CGRectContainsPoint(tableFrame, activeFieldLowerEnd) ) {
CGPoint scrollPoint = CGPointMake(0.0, activeFieldLowerEnd.y - tableFrame.size.height);
[self setContentOffset:scrollPoint animated:YES];
}
}

最佳答案

一定会成功的。它对我来说适用于过去 50 个应用程序。试试这个

[[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{


[UIView animateWithDuration:0.5f delay:0.0f options: UIViewAnimationOptionCurveEaseInOut animations:^{
_scrollView.contentOffset = CGPointMake(0, txtId_.frame.origin.y - 200);
} completion:^(BOOL finished){ }];

}

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


[UIView animateWithDuration:0.5f delay:0.0f options: UIViewAnimationOptionCurveEaseInOut animations:^{
_scrollView.contentOffset = self.view.frame.origin;
} completion:^(BOOL finished){ }];
}

关于iOS:键盘问题背后的表格 View 隐藏部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20299187/

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