Val*_*din 5 objective-c uitableview uiscrollview ios
我有UITableView.我想添加一个UITextField上面的tableView,可以通过拉下tableView来访问它.我想通过拉取tableView来隐藏我的textField.我怎样才能做到这一点?这是我试过的:
[self.messagesTableView addSubview:self.messageField];
- (UITextField*)messageField
{
if (!_messageField)
{
_messageField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.messagesTableView.frame.size.width, kMessageFieldHeight)];
_messageField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_messageField.backgroundColor = [UIColor greenColor];
}
return _messageField;
}
- (void)scrollViewDidScroll:(UIScrollView*)scrollView
{
if (scrollView == self.messagesTableView)
{
CGRect newFrame = self.messagesTableView.frame;
newFrame.origin.y = self.messagesTableView.contentOffset.y + kMessageFieldHeight;
self.messagesTableView.frame = newFrame;
}
}
Run Code Online (Sandbox Code Playgroud)
Nir*_*rav 14
我在我的应用程序中完成了这样的功能.我做了什么只是按照步骤.
1)添加one view to negative position of tableView.在此视图中,您可以根据需要随意添加textField或按钮.
UIView *viewForSearchBar = [[UIView alloc]initWithFrame:CGRectMake(0, -50, 320, 50)];
viewForSearchBar.backgroundColor = [UIColor clearColor];
[self._tableView addSubview:viewForSearchBar];
self._tableView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
Run Code Online (Sandbox Code Playgroud)
2)现在当用户开始拖动tableview(表视图的实际滚动视图)时,你可以调用scrollview的委托方法来测试它.
当你向下拖动/滚动tableView时,你会得到contentOffset.y将小于0,我在这里用代码解释.
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
if (decelerate) {
[txtSearch resignFirstResponder];
}
id<UILayoutSupport> topLayoutGuide = self.topLayoutGuide;
if(scrollView.contentOffset.y < 0)
{
UIView* hiddenHeader = ...; // this points to the hidden header view above
CGRect headerFrame = [hiddenHeader frame];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
self._tableView.contentInset = UIEdgeInsetsMake(headerFrame.size.height + [topLayoutGuide length], 0, 0, 0);
[UIView commitAnimations];
} else {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
self._tableView.contentInset = UIEdgeInsetsMake([topLayoutGuide length], 0, 0, 0);
[UIView commitAnimations];
}
}
Run Code Online (Sandbox Code Playgroud)
正如我已实施的那样,这两个步骤对我来说很好.让我添加图片来验证它.


如果您还有任何疑问,可以问我.
| 归档时间: |
|
| 查看次数: |
3841 次 |
| 最近记录: |