0

I have a UISearchBar in my navigation controller bar title view. I followed the first answer here How to add search bar in navigation bar for iphone?. This works for me but the dimensions of the search bar are hardcoded. How can I set the dimensions so that it looks nice on iPad and iPhone in both portrait and landscape mode?

This is the code in my search bar controller.

self.view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 310.0, 44)];
self.view.autoresizingMask = 0;
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(-5.0, 0.0, 320.0, 44.0)];
_searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_searchBar.delegate = self;
[self.view addSubview:_searchBar];

Then in my main viewController I have

self.navigationItem.titleView = _searchController.view;
Community
  • 1
  • 1
user1802143
  • 14,662
  • 17
  • 46
  • 55

1 Answers1

-1

There are many ways to do this, and here are some articles that may be helpful:

  1. Get the device's orientation, then use different sets of values accordingly
  2. Figure out which device the app is running on
  3. Get the dimension of the screen
Community
  • 1
  • 1
Lawrence H
  • 467
  • 4
  • 10