简体   繁体   English

iOS UITableView内容偏移量在iOS7中不起作用

[英]iOS UITableView content offset not working in iOS7

In my app I have a search bar in the header of my UITableView . 在我的应用程序中,我的UITableView标题中有一个搜索栏。 I tried to set the content offset to my UITableView for hiding the search bar but it is giving me some problems. 我试图为UITableView设置内容偏移量以隐藏搜索栏,但这给我带来了一些问题。

Finally I solved like this: 最后我这样解决了:

- (void)viewWillAppear:(BOOL)animated
{
    [self performSelector:@selector(hideSearchBar) withObject:nil afterDelay:0.0f];
}

- (void)hideSearchBar 
{
    self.tblView.contentOffset = CGPointMake(0, 40);
}

The problem is it only works for iOS 8. 问题在于它仅适用于iOS 8。

How can I achieve this correctly to work for both iOS 7 & 8 ???? 我如何才能正确实现此功能以使其同时适用于iOS 7和8?

Sorry for my poor english. 对不起,我英语不好。 Thanks in advance. 提前致谢。

If you set the header by self.tblView.tableHeaderView = yourSearchBar (recommended way),try 如果您通过self.tblView.tableHeaderView = yourSearchBar设置标题(推荐方式),请尝试

self.tblView.tableHeaderView = nil;

or 要么

self.tblView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];

to hide it. 隐藏它。

ps. PS。 yourSearchBar should be a instance variable or a property to be displayed conveniently in the future. yourSearchBar应该是实例变量或属性,以便将来方便显示。

- (void)viewDidLoad
{
    [super viewDidLoad];

    UISearchBar *bar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 40)];

    self.tableView.tableHeaderView = bar;

}

- (void)viewWillAppear:(BOOL)animated
{
    [self performSelector:@selector(hideSearchBar) withObject:nil afterDelay:0.0f];
}

- (void)hideSearchBar
{
    self.tableView.contentOffset = CGPointMake(0, -24);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM