我的应用程序中有 UISearchBar。我将其设置为表头 View 。当文本开始编辑时,我想将搜索栏设置为 viewController 的 subview 。搜索栏正确添加到 viewController 的 View ,但其框架不正确。这是我的代码:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
UITableView *tableView = ((UITableView *)[((SectionViewController *)sharedInstance.currentViewController).view viewWithTag:1111]);
[tableView reloadData];
if([searchBar.text isEqualToString:@"Filter..."])
{
searchBar.text = @"";
for(UIView *subView in searchBar.subviews){
if([subView isKindOfClass:UITextField.class]){
[(UITextField*)subView setTextColor:[UIColor blackColor]];
}
}
}
[((SectionViewController *)sharedInstance.currentViewController).view addSubview:searchBar];
[searchBar setFrame:CGRectMake(0, 20, 320, 38)];
[((SectionViewController *)sharedInstance.currentViewController).navigationController setNavigationBarHidden:YES animated:YES];
[searchBar setShowsCancelButton:YES animated:YES];
}
我想设置帧 (0, 20, 320, 38),但我得到 (0, 0, 320, 38)。另外,searchBar 的初始帧是(0, 0, 320. 38)。请告诉我为什么会得到这个?
最佳答案
UISearchBar 很奇怪。您无法自由设置其框架。这是由于内部实现造成的。
关于ios - UI 搜索栏。无法更改框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21025982/