UITextField within UISearchBar in iOS 7

后端 未结 7 1674
北荒
北荒 2020-12-16 01:00

I am trying to accomplish the same look of my UISearchBar with a TextField within it, as in my iOS 6 app. I have tried to code it in several ways and not yet been successful

7条回答
  •  情歌与酒
    2020-12-16 01:06

    Thanx to spotdog13. I finally managed to make it work for iOS 7 properly in the following way:

    #define TABLE_BOTTOM_MARGIN 5
    #define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
    #define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
    #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
    #define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
    #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
    
    @interface HomeViewController ()
    
    @end
    
    @implementation HomeViewController
    
    @synthesize searchBar;
    @synthesize searchResults;
    
    - (void)viewDidLoad
    {
     if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
    
    
        [searchBar sizeToFit]; // standard size
        searchBar.delegate = self;
    
        // Add search bar to navigation bar
        self.navigationItem.titleView = searchBar;
        }
    }
    
    - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    // Manually activate search mode
    // Use animated=NO so we'll be able to immediately un-hide it again
    [self.searchDisplayController setActive:YES animated:NO];
    // Hand over control to UISearchDisplayController during the search
    // searchBar.delegate = (id )self.searchDisplayController;
    
    return YES;
    }
    
    #pragma mark 
    - (void) searchDisplayControllerDidBeginSearch:(UISearchDisplayController
     // Un-hide the navigation bar that UISearchDisplayController hid
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    }
    
    - (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController
                                               *)controller {
    searchBar = (UISearchBar *)self.navigationItem.titleView;
    // Manually resign search mode
    [searchBar resignFirstResponder];
    // Take back control of the search bar
    searchBar.delegate = self;
    }
    

提交回复
热议问题