我正在开发iOS应用程序。它有搜索栏和右侧栏按钮项(几个按钮)。
这些东西初始化如下:
UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
UISearchBar *searchBar = searchController.searchBar;
[searchBar sizeToFit];
searchBar.placeholder = @"Search";
UIButton *routeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[routeButton setBackgroundImage:[UIImage imageNamed:@"go_route"] forState:UIControlStateNormal];
routeButton.frame = CGRectMake(0., 0., 30., 30.);
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cancelButton setBackgroundImage:[UIImage imageNamed:@"cancel"] forState:UIControlStateNormal];
cancelButton.frame = CGRectMake(0., 0., 30., 30.);
UIBarButtonItem *routeButtonItem = [[UIBarButtonItem alloc] initWithCustomView:routeButton];
UIBarButtonItem *cancelButtonItem = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];
self.navigationItem.rightBarButtonItems = @[cancelButtonItem, routeButtonItem];
self.navigationItem.titleView = searchBar;
从功能上讲,它按预期工作,但看起来有点奇怪:按钮的绘制比搜索栏略高:
我希望按钮稍微低一些,以便与搜索栏垂直对齐。我如何实现它?
P.S。
似乎如果我改变cancelButton.frame = CGRectMake(0., y, 30., 30.);
中的y坐标 - 它没有任何效果。
答案 0 :(得分:0)
尝试在viewDidAppear中添加您的代码,如下所示,
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// Enter your code
}
您将导航项目居中,如果框架未更新,请尝试
在viewDidAppear中更新其“y”位置,否则请尝试如下设置框架与搜索栏完全相邻,
routeButton.center = CGPointMake(routeButton.center.x, searchBar.center.y);