Ios8 搜索栏在viewDidLoad处具有更宽的框架
我以编程方式创建了Ios8 搜索栏在viewDidLoad处具有更宽的框架,ios8,frame,uisearchbar,uisearchcontroller,Ios8,Frame,Uisearchbar,Uisearchcontroller,我以编程方式创建了UISearchController,并为添加到其中的搜索栏设置了框架。但是搜索栏有更宽的框架,一旦我点击它,点击带有搜索栏的取消按钮;它恢复了正确的帧。如何在viewDidLoad中正确设置它 守则如下: searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 310, 44)]; searchControllerMain = [[UISearchController alloc] initWithSea
UISearchController
,并为添加到其中的搜索栏设置了框架。但是搜索栏
有更宽的框架
,一旦我点击它,点击带有搜索栏的取消
按钮;它恢复了正确的帧。如何在viewDidLoad
中正确设置它
守则如下:
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 310, 44)];
searchControllerMain = [[UISearchController alloc] initWithSearchResultsController:self.searchResultController];
searchControllerMain.searchBar.delegate = self;
searchControllerMain.searchResultsUpdater = self;
[searchControllerMain.searchBar sizeToFit];
[searchControllerMain.searchBar setPlaceholder:@"events"];
searchControllerMain.searchBar.barTintColor = [UIColor clearColor];
searchControllerMain.searchBar.searchBarStyle = UISearchBarStyleMinimal;
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setBackgroundColor:[UIColor lightGrayColor]];
[self.searchResultController.tableView reloadData];
UIView *searchBarContainer = [[UIView alloc] initWithFrame:searchBar.frame];
[searchBarContainer addSubview:searchControllerMain.searchBar];
self.navigationItem.rightBarButtonItem= [[UIBarButtonItem alloc] initWithCustomView:searchBarContainer];
下面是我如何在导航项的标题视图中放置
搜索栏的方法
self.searchBar = [UISearchBar new];
self.searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
self.searchBar.placeholder = @"Search gospel events";
self.searchBar.delegate = self;
self.navigationItem.titleView = self.searchBar;
正如你所看到的,我没有设置框架。它从正确的初始大小开始,并将随着方向的变化调整大小
您是否尝试过设置显式的自动ResizingMask
?我从未尝试过将其放置在自定义栏按钮项中,因此我不确定这是否也能解决您的自定义容器问题。尝试以下方法:
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
[searchControllerMain.searchBar setFrame:CGRectMake(0, 0, 310, 44)];
[searchControllerMain.searchBar setBounds:CGRectMake(0, 0, 310, 44)];
}
这对我有用 请把你的密码寄出去好吗?如果没有它,就很难知道你做错了什么,也可以避免诸如“你是否正确设置了帧大小?”等问题……你可以只替换searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0,0,310,44)]代码>带有searchBar=[[UISearchBar alloc]init]
并检查它是否解决了您的问题?也解决了。还为searchBarContainer
提供了框架,但这两种方法都不起作用!您应该设置searchControllerMain.searchBar
的框架,而不仅仅是searchBar
UISearchController
为您创建搜索栏,您不需要自己分配它。searchControllerMain.searchBar
是只读属性。我无法为它设置框架。另外,如果我没有为searchBar
设置框架,并使用searchControllerMain.searchBar
中的默认搜索栏,它现在会将其框架加宽到左角。这对我不起作用……我曾尝试为搜索栏提供自动重新设计任务,但这也没有什么意义。