gpt4 book ai didi

ios - 以编程方式创建搜索 View ios

转载 作者:行者123 更新时间:2023-11-29 11:04:01 24 4
gpt4 key购买 nike

我目前正在尝试在 ios 中使用 tableview 和 searchbar 以编程方式创建一个 searchview。 tableview 按预期工作,但搜索栏在屏幕左侧,只有最后一个像素可见。请告诉我哪里错了。

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

//init array item for table view
self.timeZoneNames = [NSMutableArray array];
for(int i = 0; i < 100; i++)
{
[self.timeZoneNames addObject:[NSString stringWithFormat:@"a %d", i]];
}

//init rectangel that store table view's frame
UIScreen *mainScreen;
CGRect rect = CGRectMake(0, 44, [mainScreen applicationFrame].size.width,[mainScreen applicationFrame].size.height);

//init search bar
UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, [mainScreen applicationFrame].size.width, 44)];

//init search controller that handle search bar
//UISearchDisplayController *searchController = [[UISearchDisplayController alloc] initWithSearchBar:search contentsController:self];
//searchController.delegate = self;
//searchController.searchResultsDataSource = self;

//UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [mainScreen applicationFrame].size.width, 44)];
//[view addSubview:search];

//init table view, add search bar on top of table view and reload data
UITableView *table = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
table.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
table.dataSource = self;
table.delegate = self;
//table.tableHeaderView = search;
[table reloadData];

//set table view to be this controller's view
//self.view = table;
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [mainScreen applicationFrame].size.width, [mainScreen applicationFrame].size.height)];

self.view.backgroundColor = [UIColor blackColor];
[self.view addSubview:table];
[self.view addSubview:search];
}

最佳答案

您可以使用 self.view.bounds 而不是 [mainScreen applicationFrame]。而且您不必再次为 self.view 分配内存。您可以删除该部分。

关于ios - 以编程方式创建搜索 View ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14471451/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com