iosswiftuikituisearchcontroller

Adjust insets of UISearchController's searchBar


I'm trying out some designs and I'd like to have an equal 25px margin for all my subviews. UISearchController's searchBar has a 16px padding on either sides out of the box. Do I have to write the search bar textfield from scratch, or is there ANY way, undocumented, swizzling, or otherwise, to accomplish offsetting the native bar to my taste?

enter image description here


Solution

  • If you are using a UISearchController you can set the margins of the searchBar like this (assuming searchBar is the UISearchController instance):

    let directionalMargins = NSDirectionalEdgeInsets(top: 0, leading: 25, bottom: 0, trailing: 25)
    searchController.searchBar.directionalLayoutMargins = directionalMargins