Search code examples
swiftwidthsearchbar

How to change searchBar width programmatically? (Swift)


I'd like to resize my app components when device is turned into left or right side - when it keep horizontal position

I'm checking device rotation in willRotateToInterfaceOrientation event and then I have to change width of searchBar and tableView. How should I do it? I tried some variants with components .frame and with CGRect, nothing works. enter image description here

Update: it can be done without any code. View my answer below


Solution

  • If you laid out your elements in Interface Builder then you can just add an outlet to the width constraint of your search bar. Then, change the constraint's constant property to change the bar's width.