【发布时间】:2014-09-29 23:15:20
【问题描述】:
我决定在我的应用程序中使用表头视图来保存搜索栏和 UISegmentedControl。这是视图控制器的viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 65, 320, 44)];
[self.searchBar sizeToFit];
[self.searchBar setUserInteractionEnabled:YES];
[self setSearchController:[[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self]];
self.mainSegment = [[UISegmentedControl alloc] initWithItems:@[@"YouTube", @"iTunes"]];
[self.mainSegment setFrame:CGRectMake(8, 109, 305, 29)];
[self.mainSegment setSelectedIndex:0];
[self.mainSegment addTarget:self action:@selector(searchTypeChanged:) forControlEvents:UIControlEventValueChanged];
[self.mainSegment setUserInteractionEnabled:YES];
UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)];
[headerView setBackgroundColor:[UIColor clearColor]];
[headerView addSubview:self.searchBar];
[headerView addSubview:self.mainSegment];
[headerView bringSubviewToFront:self.searchBar];
[headerView bringSubviewToFront:self.mainSegment];
[headerView setUserInteractionEnabled:YES];
self.tableView.tableHeaderView = headerView;
self.tableView.userInteractionEnabled = YES;
}
这会产生一个很好的结果:
但是,我无法与搜索栏或分段控件交互。如上所示,我尝试将 userInteractionEnabled 设置为 YES,但问题仍然存在。有什么想法吗?
【问题讨论】:
-
无响应的控件通常是因为它们超出了它们的超级视图的范围。为您的标题视图提供不同的背景颜色,以查看它们是否在边界内。
-
@rdelmar 已经解决了,谢谢!
标签: ios uitableview uisearchbar uisegmentedcontrol