How to set textfield in UISearchBar?

iOS 7 and later, the UITextField in UISearchBar should be found using the way:

1 // Set Search Button Title to Done 2

for (UIView *searchBarSubview in [self.searchBar subviews]){
    if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)])
    {
        // Before iOS 7.0 5
        @try {
            [(UITextField *)searchBarSubview setReturnKeyType:UIReturnKeyDone];
            [(UITextField *)searchBarSubview setKeyboardAppearance:UIKeyboardAppearanceAlert];
        }
        @catch (NSException * e) {
            // ignore exception11
        }
    }
    else {
        // iOS 7.014
        for(UIView *subSubView in [searchBarSubview subviews]){
            if([subSubView conformsToProtocol:@protocol(UITextInputTraits)])
            {
                @try {
                    [(UITextField *)subSubView setReturnKeyType:UIReturnKeyDone];
                    [(UITextField *)searchBarSubview setKeyboardAppearance:UIKeyboardAppearanceAlert];
                }
                @catch (NSException * e) {
                    // ignore exception22
                }
            }
        }
    }
}



the code is from: http://www.tuicool.com/articles/FjYjaq

Thanks,

Blues


你可能感兴趣的:(How to set textfield in UISearchBar?)