3 Replies
      Latest reply on May 2, 2019 5:47 AM by KamalGeethikasri
      KamalGeethikasri Level 1 Level 1 (0 points)

        We have developed an application for iOS 5 long back where we used 'Search Bar and Search Display Controller' object on storyboard. Since iOS8 UISearchDisplayController is deprecated, I am trying to remove existing 'Search Bar and Search Display Controller' with UISearchController.

        As UISearchController is not available from 'Object Library' on interface, I have added it programatically to an UIView on interface using following code:

        //Set up Search Controller
        self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
        self.searchController.searchResultsUpdater = (id)self;
        self.searchController.dimsBackgroundDuringPresentation = YES;
        self.searchController.searchBar.delegate = self;
         
        //Adding as subview to UIView 'searchBarContainer'
        [self.searchBarContainer addSubview:self.searchController.searchBar];
        [self.searchBarContainer bringSubviewToFront:self.searchController.searchBar];
        
        self.definesPresentationContext = YES;
        self.extendedLayoutIncludesOpaqueBars = YES;

         

        And set the frame of 'searchController' equal to the UIView as follows:

        UISearchBar *ctrlSearchBar = self.searchController.searchBar;
        ctrlSearchBar.frame = CGRectMake(0, 0, self.searchBarContainer.frame.size.width, self.searchBarContainer.frame.size.height);

         

        The size of searchBar is fine but when it is focused on clicking on it, the width of 'searchBar' increased automatically.

        I even tried using NSLayoutConstraints but didn't fix the issue. Can anyone please help how to fix the size of 'UISearchController.searchBar' even when it is focused?