You need to wrap the UISearchBar with another view.
UISearchBar *searchBar = [UISearchBar new];
UIView *searchBarContainer = [[UIView alloc] initWithFrame:searchBar.frame];
[searchBarContainer addSubview:searchBar];
UIBarButtonItem *searchBarItem =
[[UIBarButtonItem alloc] initWithCustomView:searchBarContainer];
source
share