【问题标题】:iOS UITableView content offset not working in iOS7iOS UITableView 内容偏移在 iOS7 中不起作用
【发布时间】:2015-05-14 08:40:52
【问题描述】:

在我的应用程序中,myUITableView 的标题中有一个搜索栏。我尝试将内容偏移设置为我的UITableView 以隐藏搜索栏,但这给我带来了一些问题。

最后我这样解决了:

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

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

问题是它只适用于 iOS 8。

我怎样才能正确实现这一点以同时适用于 iOS 7 和 8 ????

对不起,我的英语很差。提前致谢。

【问题讨论】:

标签: ios objective-c uitableview ios7 contentoffset


【解决方案1】:

如果你用self.tblView.tableHeaderView = yourSearchBar设置header(推荐方式),试试

self.tblView.tableHeaderView = nil;

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

隐藏它。

ps。 yourSearchBar 应该是实例变量或者属性,方便以后显示。

【讨论】:

  • 我不想隐藏所有标题。在标题中,我有比我想要显示的搜索栏更多的项目。搜索栏位于最顶部。我只想在某个位置显示 tableview 只隐藏搜索栏。
  • @user3065901 可能你不应该把它设置为tableView的headerView。相反,您可以将 toolBarView 作为其子视图添加到 tableView 的 superView。然后在scrollViewDidScroll:方法中改变toolBarView的frame。
【解决方案2】:
- (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);
}

【讨论】:

  • 这适用于 iOS 8 但不适用于 iOS7。我也想知道如何在 iOS7 上实现这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-21
  • 1970-01-01
  • 1970-01-01
  • 2015-04-02
  • 1970-01-01
相关资源
最近更新 更多