How to hide/Remove the search bar on Contact Picke

2019-02-27 10:54发布

问题:

I am adding a contact picker in my app, however, I do not want the search functionality.

How to hide/Remove the search bar on Contact Picker (ABPeoplePickerNavigationController)?

回答1:

static BOOL foundSearchBar = NO;
- (void)findSearchBar:(UIView*)parent mark:(NSString*)mark {

    for( UIView* v in [parent subviews] ) {

        //if( foundSearchBar ) return;

        NSLog(@"%@%@",mark,NSStringFromClass([v class]));

        if( [v isKindOfClass:[UISearchBar class]] ) {
            [(UISearchBar*)v  setTintColor:[UIColor blackColor]];
            v.hidden=YES;
//            foundSearchBar = YES;
            break;
        }
        if( [v isKindOfClass:[UITableView class]] ) {
            CGRect temp =v.frame;
            temp.origin.y=temp.origin.y-44;
            temp.size.height=temp.size.height+44;
            v.frame=temp;
            //foundSearchBar = YES;
            break;
        }
        [self findSearchBar:v mark:[mark stringByAppendingString:@"> "]];
    }
}

call above method after picker is presented as below:

-(void)showPeoplePickerController
{
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
    picker.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    // Display only a person's phone, email, and birthdate
    NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty], 
                                [NSNumber numberWithInt:kABPersonEmailProperty],
                                [NSNumber numberWithInt:kABPersonBirthdayProperty],[NSNumber numberWithInt:kABPersonAddressProperty],nil];

    picker.displayedProperties = displayedItems;
    // Show the picker
    [self presentViewController:picker animated:YES completion:nil];
    [self findSearchBar:[picker view] mark:@"> "];

    [picker release];
}


回答2:

-(void)showAddressBook {
    ABPeoplePickerNavigationController *addressBook = [[ABPeoplePickerNavigationController alloc] init];
    [addressBook setPeoplePickerDelegate:self];
    addressBook.delegate = self;
    addressBook.navigationBar.topItem.title = @"iPhone Contacts";
    UIView *view = addressBook.topViewController.view;
    for (UIView *v in view.subviews) {
        if ( [v isKindOfClass:[UITableView class]] ) {
            CGRect temp = v.frame;
            temp.origin.y = temp.origin.y - 44;
            temp.size.height = temp.size.height + 44;
            v.frame = temp;
        }
    }
    [addressBook release];
}

- (void)navigationController:(UINavigationController*)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if ([navigationController isKindOfClass:[ABPeoplePickerNavigationController class]]) {
        UISearchDisplayController *searchDisplayController = navigationController.topViewController.searchDisplayController;
        [searchDisplayController.searchBar setHidden:YES];
    }
}