ios 使用故事板时如何使 UITable 标题粘在屏幕顶部?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14376697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make UITable header stick to the top of the screen when using Storyboards?
提问by Egor
I've made header using this post: Table Header Views in StoryBoards
我已经使用这篇文章制作了标题:StoryBoards 中的表标题视图
But i am unable to make header stick (fix) to the top of the screen while scrolling.
但是我无法在滚动时将标题粘贴(固定)到屏幕顶部。
How can that be achieved?
如何实现?
P.S. Table style is plain, and when i tired to overload viewForHeaderInSection
and returned outlet for header view, it just got worse.
PS 表格样式很简单,当我厌倦了超载viewForHeaderInSection
并返回插座以进行标题视图时,情况变得更糟。
Thanks in advance!
提前致谢!
采纳答案by Egor
I was able to do it (at least in iOS 6) like this, don't know why this thing works:)
我能够这样做(至少在 iOS 6 中)像这样,不知道为什么这件事有效:)
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if(self.headerManualView == nil) {
//allocate the view if it doesn't exist yet
headerManualView = [[UIView alloc] init];
//create the button
UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(10, 3, 250, 44)];
//the button should be as big as a table view cell
txtField.borderStyle = UITextBorderStyleRoundedRect;
//set action of the button
//[txtField addTarget:self action:@selector(removeAction:) forControlEvents:UIControlEventTouchUpInside];
//add the button to the view
[headerManualView addSubview:txtField];
}
//return the view for the footer
return headerManualView;
}
回答by A-Live
Here's the code which I believe makes the header view stick to the table top (that is not it's default behavior):
这是我认为使标题视图粘在桌面上的代码(这不是默认行为):
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect rect = self.tableHeaderView.frame;
rect.origin.y = MIN(0, self.tableView.contentOffset.y);
self.tableHeaderView.frame = rect;
}
An alternative is to have header view for the first section. You can't use any subviewat viewForHeaderInSection
, you have to return there the view which is not yet at views hierarchy. The advantage of the method is that the section header view is designed to be at the table top, the drawback is that you might want to have headers for sections in the feature, it will break the solution.
另一种方法是为第一部分设置标题视图。您不能在 处使用任何子视图viewForHeaderInSection
,您必须在那里返回尚未处于视图层次结构中的视图。该方法的优点是部分标题视图设计在桌面,缺点是您可能希望功能中的部分具有标题,这会破坏解决方案。
回答by Crystian Le?o
I think it's better for you to use a UIViewController, instead of a UITableViewController in the storyboard, to achieve this. Just put a header in the top of the view, in the storyboard, and put a UITableView under the header.
我认为最好使用 UIViewController 而不是故事板中的 UITableViewController 来实现这一点。只需在视图顶部的故事板中放置一个标题,然后在标题下放置一个 UITableView。