I've tested on simulator of iOS 7.1. I see structure of UISearchBar like this:
===============================================
UISearchBar
++ UIView
++++ UISearchBarBackground
++++ UISearchBarTextField (This is UITextField object)
++++++++ _UISearchBarSearchFieldBackgroundView
++++++++ UIImageView
===============================================
So on iOS 7.1, you can get UITextField like this:
UIView *subviews = [searchbar.subviews lastObject];
UITextField *textView = (id)[subviews.subviews objectAtIndex:1];
===============================================
UPDATE
If your UISearchBar
is in a UIViewController
, try your code in method viewDidLoad
or viewWillAppear
!
Don't look for UITextField
as soon as you create UISearchBar
(look for after UISearchBar
has already appeared on screen)