Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 231
Oscar Apeland Avatar asked Oct 30 '25 06:10

Oscar Apeland


2 Answers

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
like image 112
kzozfsdg Avatar answered Nov 01 '25 19:11

kzozfsdg


For UISearchBar text position use:

searchBar.searchTextPositionAdjustment = .init(horizontal: 25, vertical: 0)
like image 30
Rodrigo Cámara Avatar answered Nov 01 '25 18:11

Rodrigo Cámara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!