UITextField within UISearchBar in iOS 7

后端 未结 7 1666
北荒
北荒 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 <UISearchBarDelegate>)self.searchDisplayController;
    
    return YES;
    }
    
    #pragma mark <UISearchDisplayDelegate>
    - (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;
    }
    
    0 讨论(0)
  • 2020-12-16 01:07

    Try this out i am not sure but this should work as in iOS 7 searchbar has subview and inside that subview there are two subviews one of which is UITextField

    UIView *searchbarview = [searchBar.subviews objectAtIndex:0];
    UITextField *sbTextField = (UITextField *)[searchbarview.subviews lastObject];
    [sbTextField removeFromSuperview];
    
    CGRect rect = searchBar.frame;
    rect.size.height = 32;
    rect.size.width = 210;
    sbTextField.frame = rect;
    
    [sbTextField setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin];
    
    
    UIBarButtonItem *searchBarNavigationItem = [[UIBarButtonItem alloc] initWithCustomView:sbTextField];
    
    [[self navigationItem] setLeftBarButtonItem:searchBarNavigationItem];
    
    0 讨论(0)
  • 2020-12-16 01:11

    iOS6 & iOS7 compatible solution:

    - (void)setTextFieldAsDelegate:(UIView *)inputView {
        for (UIView *view in inputView.subviews) {
            if ([view isKindOfClass:[UITextField class]]) {
                searchBarTextField = (UITextField *)view;
                searchBarTextField.delegate = self;
                break;
            } else {
                [self setTextFieldAsDelegate:view];
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-16 01:15

    You should not put a UITextField in the UINavigationBar in iOS 7, this widget is already provided by Apple.

    In iOS 7, you can simply use a UISearchDisplayController with a UISearchBar, and set:

    searchDisplayController.displaySearchBarInNavigationBar = YES
    

    The search bar will appear in your UINavigationBar, and it will play nice with the other UIBarButtonItems without all the hacks and manual frame sizing in your original iOS 6 solution.

    One thing to note - if you are going to add this to a project that still supports OSes older than iOS 7, you'll want to make sure that you put a check around the call or your app will crash when running on older OSes.

    if([searchDisplayController respondsToSelector:@selector(displaysSearchBarInNavigationBar)])
    {
        searchDisplayController.displaysSearchBarInNavigationBar = YES;
    }
    

    See this section of the iOS 7 transition guide: https://developer.apple.com/library/ios/documentation/userexperience/conceptual/TransitionGuide/Bars.html

    In iOS 7, UISearchDisplayController includes the displaysSearchBarInNavigationBar property, which you can use to put a search bar in a navigation bar, similar to the one in Calendar on iPhone:

    One other note - you should consider migrating to AutoLayout going forward so you don't have to do all that tedious frame manipulation. Apple recommends it, and probably for good reason (future devices with larger screens...?)

    0 讨论(0)
  • 2020-12-16 01:23

    in iOS 7 to access Text Field you have to reiterate on level more. Change your code like this

    for (UIView *subView in self.searchBar.subviews){
        for (UIView *ndLeveSubView in subView.subviews){
        if ([ndLeveSubView isKindOfClass:[UITextField class]])
            {
                searchBarTextField = (UITextField *)ndLeveSubView;
                break;
            }
        }
       }
    

    But best way to clear backgournd of UISearchBar and setting searchbar icon in text field is:

    [searchBar setBackgroundImage:[[UIImage alloc] init] ];//if you want to remove background of uisearchbar
    UIImage *image = [UIImage imageNamed: @"search_icon.png"];
    [searchBar setImage:image forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    
    0 讨论(0)
  • 2020-12-16 01:27

    Create a UIView *textFieldContainer with your target frame, add your textfield to that UIView and then add that textFieldContainer as a navigation item. i.e. your approach remains the same just the textfield comes inside a container and you play with that container.

    0 讨论(0)
提交回复
热议问题