Popover will do a great job when presenting  useful information on the context of vriewcontroller. However we often have to display the popover in fixed size to make it look better and convinent and userfriendly. Adding  contentSizeForViewInPopover could have done the job before iOS 7. but there is a new method introduced in ios 7 to the same job to be supported in all the feature versions.  Add the code below to check if the code running in iOS 7 and later it will use  the  latest method otherwise it will use the old method to adjust the popover to a fixed size.

if ([[[UIDevicecurrentDevice] systemVersion] floatValue] >= 7.0f)
popover.preferredContentSize=CGSizeMake(width, height);
else
popover.contentSizeForViewInPopover = CGSizeMake(width, height);

Here is the Example how search popover can be presented and used.

#pragma mark Searching
- (void) searchTapped: (UIButton *) sender
{
    SearchViewController *searchController=[SearchViewController new];
    UIPopoverController *popOver;
    popOver = [[UIPopoverController alloc] initWithContentViewController:searchController];
    [searchController.searchBar becomeFirstResponder];
    popOver.delegate = self;
    [popOver presentPopoverFromRect:CGRectMake(0, 0, sender.frame.size.width, sender.frame.size.height) inView:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    self.popoverController = popOver;
    [popOver release];
    
}

Here is the Search controller implementation

@implementation SearchViewController

@synthesize delegate, searchBar;

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 350, 50)];
    searchBar.delegate = self;
    searchBar.placeholder = @"Enter keyword to search";
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f){
        self.preferredContentSize=CGSizeMake(searchBar.frame.size.width, searchBar.frame.size.height);
    searchBar.barTintColor = [UIColor colorWithWhite:.91 alpha:1];
    }
    
    [self.view addSubview:searchBar];
    [self setContentSizeForViewInPopover:searchBar.frame.size];
        
    [searchBar release];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    
    [self.delegate didEnterSearchQuery:searchText];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *) searchBar
{
    [searchBar resignFirstResponder];
    // Capture here
    [self.delegate didFinishSearchQuery:searchBar.text];
}


- (void)viewWillAppear:(BOOL)animated
{
    SearchBar.text = [delegate startSearchQuery];
}

- (void) clearSearch
{
    [searchBar setText:@""];
}


@end

  • Hero

    good one