Rotating UIView and background to landscape mode - iphone

The initial root view controller is 9 (3x3) buttons with a custom background in portrait mode. When the device is rotated into landscape mode the last row of buttons (of course) are cut off and the background (which is 320x480) doesn't fill the width of the screen.
What is the proper way of handling this? Do I need to move and resize the buttons myself? Do I have multiple Nibs? How do I resize/rotate the background?

Use shouldAutorotateToInterfaceOrientation and/or deviceDidRotateSelector in your viewController to reposition / layout & scale your views programmatically as you require.

If you set the auto-resizing masks on the buttons to have flexible margins in all directions they should reposition themselves appropriately when you rotate the device. You can do this either in code, or in Interface Builder (which has a nice little simulator demonstrating the effect)
With regards the background, what I prefer to do is to make the image the combination of the maximum proportions of each orientation, and then center it. For instance, make your image 480 x 480, make the view that houses it the same size and then use the appropriate auto-resizing masks to achieve the desired effect.

Related

Xcode storyboard view on rotation

I would like to have a view to the top or left of the device.
I have tried using a stack view to rotate the view but I was unable to use all interface orientations.
Example: the iPad orientation has a regular width and height in both portrait and landscape orientation. Thus I believe I cannot rotate the stack view axis on this device.
What are other options to keep the view how I want it preferably using the storyboard with auto-layout only but if needed using swift code.
Edit:
To clarify, both the blue and grey view can be seen as empty UIView for the purpose of this question.
To clarify, when you rotate the device, you want the text, buttons, etc to rotate but the background to stay the same (as in the view takes up the same screen area, but things in the view are rotated), correct?
If so, use size classes to give your views different constraints for different views

Is it possible to set autosizing in interface builder for rotatable bg image?

I'm designing the UI with IB with a lot of images, and the default design orientation is landscape. However, I need to shrink the whole view to fit in portrait mode, by shrinking to the maximum allowed width and maintaining same aspect ratio for the view with it's inner images. The best way to imagine what I want to achieve is to imagine some landscape photo. When you rotate that to portrait it shrinks to the maximum width, centers vertically, but maintains the same aspect ratio. In my case, I want to shrink a view with its all inner subviews.
First, I'm manually resizing view frame to required size in viewWillApppear, and also calling setAutoresizesSubviews to resize all it's inner views. Those inner views - images are with "Aspect Fit" and all autosizing options set (to fill the available area on resized view). However, inner views pops out of the container: some images jumps to the top, text view expands to full portrait height. Is possible to set some kind of "view-container", then say - "view resize to size X and resize all your but only inside view-container area?
When you design in IB you must check several things. First of all the "view container" properties, that is the view that contains all subview. In order to behave properly on rotation this view must be setup with right IB Autosizing properties, that is with the autosize lines (springs) and border lines (structs) correctly set. In your case you want your container to be exactly fit with the main view so enable the "structs" at the four sides and enable the "springs" inside. Play with this values and look at the "Example" displayed by IB.
Then you must instruct this container to properly behave with its subviews. So check in IB that "Autoresize subviews" and the content mode, if needed, (= "Mode") has the wished setting.
At this point you check in the device or the simulator if your view is rotating and rising properly.
Then you must take care of the content of this view, which is a UIImageView. This must be centered, so remove the structs, and autoresized, so set the springs. Then set the content mode to Aspect Fit or Aspect Fill at your wish.
These settings should work. For more complex stuff, you must programmatically set things.

Why do my UIImageviews get resized/dimmed when using UINavigation/UITabbar controller

I have an app with a primary view that has a UITabBarController with 5 tabs. Each tab is a UINavigationController.
In interface builder, I'm customizing the background of each page by dragging a UIImageView and setting it fullscreen. The image I'm setting to the view is 640x960. I am setting it to be Aspect Fill.
However, what I've noticed is that it is not where I would expect it to be. When navigating between by tabs, the image seems to be shifted down from where it should be.
Also, when pushing a new view to the navigation controller, the background of this new view isn't offset in the same way as the tabbar one, and it is also slightly dimmed.
How can I set my UIImageViews on each page to be aspect correct and fill the screen 1:1? Also, how does one disable the dimming when pushing a view to the navigation controller?
Thanks for any tips, and apologies if this is covered in another thread, I couldn't find an answer searching the site.
Assuming that you are using the IB to setup your views, you should select navbar/tabbar options to reflect what will be on the actual page. That should place your image correctly. I would also recommend that you make both a low res and hi res version of your background images -- 320x480 and 640x960. Of course, your size may need to be adjusted (reduced) for the navbar and/or tabbar which will leave less than 960 px of vertical height -- probably more like 920px if you are in portrait mode. Then you add #2x to the base name of the hi res version, this would account for older iPhone screens.
Once you have the image placed correctly, resizing should be unnecessary. You can have the image automatically resize width and height using the little arrows on the layout page -- that's a bit hard to describe. It can also be done in code -- if you still need that I can provide a sample.
Maybe you need to set all AutoresizingMask in order to resize the UIImageView properly on each view. And to main aspect ratio u should use either AspectFill or AspectFit on the contentMode.

trouble with rotating view (and resizing elements within) in ipad application

I'm having a nightmare with the rotation on iPad. I've searched all over the place for some tutorials, but nothing seems to really be for what I want. (Possibly not searching for the right thing?!)
I have a portrait view by default which is an image and a button inside the view. When I rotate, I detect this can work out if it's landscape. I then try to set the frame size of the uiview to fit nicely on the screen.
If I let it autoresize, it simply stretches and fills the screen. This I don't want.
but the trouble is, when I resize, the button gets resized too, but not in the same ratio as the image.
My question is: What's the best way to resize the view. I wanted to simply reduce the uiview by say 60% and it resizes EVERYTHING in that view with the same 60%. The only way I see this is working at the moment is to create two views... but that's twice the work and maintenance!
I've tried messing with the autosizing arrows in Interface builder, but that again seems to screw things up more!
I'm completely lost here!! Thanks for any info
The problem you have there is that the view is automatically resized to the screen ratio. On an iPad in Portrait Orientation the screen size is 1024x768. After the rotation to Landscape the origin rotates too and your screen content is skewed or stretched to 768x1024.
What you need to do is to override the
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
message of the UIViewController of the view which rotates. This message is called within the animation block of the rotation. You just set the framesize of your subviews (the button) to whatever is best for you. Once i had a problem with rotating an OpenGL view. The content of the view was stretched when rotating to landscape. Since it is not possible to alter any OpenGL matrices within the animation block the only solution i found was to make the view quadratic and to set the origin behind the bounds of the screen (in -x direction). You have to override the message also to reset the origin above the screen (in -y direction) bounds in landscape mode, to keep the viewport in the middle of the screen. That way the view kept its ratio. Whatever solution is best for you, overriding this message should work out.
Have you tried disabling the autoresizesSubviews property on your UIView? It should prevent any size changes on the subviews when you resize your view.

What's the best way to handle landscape/portrait differences in IB?

I have a view that supports landscape and portrait viewing, with the controls all moving around when you switch from one to the other. I'm currently doing this by setting the .center of each one of my controls when the user rotates the phone. The problem is that this is tedious, and requires a lot of code, and seems to defeat the purpose of using Interface Builder in the first place.
My question is: is there a way in Interface Builder for one view to support multiple looks (one for landscape one for portrait)? If not how do other people do this with IB? Do you set up 2 views?
Edit: Just to clarify my landscape and portrait views look different, I don't want a straight transform, I actually display the data differently in landscape mode
When necessary, I add UIView objects to the view in IB which I make hidden. Give it a nice background color so you can see it, and send it all the way to the background. Then use that view's frame when you need to set the frame of an object. If you have a lot of them, you might consider using UILabel instead, so you can give it a visible name in IB.
If you're worried about memory issues, just remove all these extra UIViews in ViewDidLoad and just store their frame values in member CGRects. This only works of course if you don't have any of the views auto-resize or reposition on rotate, which you probably shouldn't anyway, in this case. I do this for resizing/repositioning for any reason, not just when the screen rotates.
I'm not 100% sure if it's possible, but have you considered using different view controllers for landscape and portrait?
The AutoSize attributes of IBOutlet objects in the Size Inspector of IB (command 3) give some pretty nice options for auto-stretching and positioning of items. You can control L/R and T/B screen positions and relative width and height. You can't get full control of the layout, but most of the basic operations are there.
The only way one view can support multiple orientations in IB is to set the autosizing mask of components to either scale and/or anchor to edges. To design a totally different layout for each orientation you need to design a portrait and landscape view separately (each in its own XIB) and switch between them programatically.