iphone - 更改 TableView 上 UISearchBar 的宽度

标签 iphone objective-c xcode ios tableview

我需要在我的 tableView 中创建两个 UISearchBar。我希望它们在 table 顶部的宽度相等(并排)。

我已经创建了 UISearchBar 的两个导出,并为它们创建了 property 和 de alloc。我发现很难将它们都放在(我的意思是适合) View 中。我只有一个搜索栏扩展到屏幕的全宽。我怎么装两个?

searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 4, 45)];
zipSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(4, 0, 4, 45)];

searchBar.placeholder = @"School Name";
zipSearchBar.placeholder=@"Zip";

searchBar.delegate = self;
zipSearchBar.delegate = self;

self.tableView.tableHeaderView= zipSearchBar;
self.tableView.tableHeaderView= searchBar;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;

最佳答案

当您执行 self.tableView.tableHeaderView= searchBar; 时,您只是重新分配它。您必须构建一个包含两个搜索栏的 View ,然后将其设置为表的标题 View 。像这样,

searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 200, 45)];
zipSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(200, 0, 120, 45)];

searchBar.placeholder = @"School Name";
zipSearchBar.placeholder=@"Zip";

searchBar.delegate = self;
zipSearchBar.delegate = self;

UIView * comboView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
[comboView addSubview:searchBar];
[comboView addSubview:zipSearchBar];

self.tableView.tableHeaderView= comboView;
[comboView release];

关于iphone - 更改 TableView 上 UISearchBar 的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6253827/

相关文章:

objective-c - 代码 : How To Adjust Components Height and Weight in Pixel?

ios - 有两个选择的数组让我只完成了一半

ios - 将 Microsoft Project Oxford 语音识别从 Objective-C 转换为 SWIFT

iphone - 收到内存警告和应用程序崩溃

ios - 设置主 TableView 行的动态高度,该行有另一个从 TableView ?

iphone - UIPickerView行图片选择高亮颜色

iphone - scheduleLocalNotification 的性能问题(大量本地通知)

ios - 加载以前从相机胶卷中保存的图像

ios - 将 NSFetchedResultsController 添加到项目后出现的问题

ios - 带有 UIImagePickerController iOS 的圆形裁剪器相机?