Rotating a view with a UISearchBar

Hey guys! I was just wondering if anyone had any luck with getting a UISearchBar to autosize after rotating your device to landscape mode. I can't get it to work in my app.

Essentially, on the first rotation to Landscape mode the rest of the view will resize correctly and the UISearchBar rotates, but stays the same width. When I change back to Portrait mode and then to Landscape mode again, the UISearchBar autoresizes correctly.

I found some images from another developer having the same problem:

User uploaded file


User uploaded file

Thanks for the help!

Posted on Jul 17, 2009 10:56 AM

Reply
3 replies

Jul 18, 2009 1:55 AM in response to jonahgabriel

I don't have 3.0 installed and don't think you mentioned your OS. I'm not having a problem with search bar autorotation on 2.x, so will let you know how it's setup over here. Maybe it will apply to 3.0 as well. If you can also test on 2.x, that would be a good sanity check--it's important to know whether you're dealing with a behavior that started in 3.0.

Firstly, if you created your view in code, it might help to make a testbed that defines your view in IB. I would use the View-Based template for the testbed (Btw, I assume you have a root view under the search bar and table view; if not, that's likely to be the problem). Next, select the search bar and bring up the Size Inspector. Make sure only the horizontal spring is on, the bottom strut is off and the other 3 struts are on (In case you're unfamiliar with these settings, see Setting a View’s Autosizing Behavior in the Interface Builder User Guide). Both the content view and table view should have both springs and all 4 struts on. Of course you can make these same settings in code with the autoresizingMask property, but the next step requires IB.

If you have IB 3.1.1 or later, locate the curved arrow in the upper right corner of the view editor window. Clicking that arrow should toggle the view to landscape and back to portrait. See if the search bar has the correct dimensions each time you toggle that arrow. Do it several times to be sure. If the behavior isn't correct, go back to the Size Inspector panel and see what the frame settings are doing as you toggle the arrow. Reset the dimensions if necessary and repeat the test.

If you continue to have a problem, check the content view and table view with the Size Inspector and see if those frames are tracking correctly. When you can toggle the arrow repeatedly without any problem, go back to portrait and save the xib.

If the above doesn't directly apply, I hope the exercise gives you some useful ideas. Sometimes just isolating the view in a testbed can lead you to the answer.

\- Ray

p.s.: I forgot to factor in a nav controller. I would recommend building the test bed without the nav bar at first. If you get it working, then put everything onto a nav controller view and see if that's where the problem starts. If so, apply the same tweeking procedure. - R

Jul 20, 2009 11:00 AM in response to RayNewbie

Hey...thanks for the response. I am using 3.0. I also forgot to mention that the resize works fine in the simulator but not on my iPhone itself. Also, I am creating everything with IB and not programatically.

Try this...

1) Create a new TabBar application.

2) Create a new ViewController and call it SecondViewController. Make it a UITableViewDelegate and UITableViewDatasource

3) Double click on Main.xib and select your Tab Bar controller. Change the type of the second tab from UIViewController to NavigationController.

4) Select the Tab Bar item and set its class to SecondViewController and its xib to SecondView.xib

5) Open up SecondView.xib, remove the existing view. Add a new View and under it, add a search bar and a table view. Set the tableview's delegate and datasource to the File's Owner and set the File's owner view to the newly created UIView.

6) Go back into both FirstViewController and SecondViewController and set their shouldAutorotateToInterfaceOrientation to return YES.

7) Now, start up the simulator, switch to tab 2 and rotate the device to landscape mode. Everything should resize correctly.

8) Next, start up the app on your device and switch to tab 2. When you rotate your device to landscape mode, your search bar will not resize correctly.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Rotating a view with a UISearchBar

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.