How to get an iphone application to resize correctly on orientation changes? - objective-c

I have an iphone app, with a view (well multiple but let's say one at a time). The view is very simple just some text-fields and images on there. I have set the auto-resizing attributes for those controls so that they know that they need to be centered no matter what the orientation is. There is no code that moves them or anything, everything is in IB.
The views only re-orient correctly if i change to landscape on the main view and then transition to the other views. If I go to any view on portrait and then try to go to landscape, the iphone performs the animation for rotation, but my controls don't seem to move to the right places. When i test the orientation in IB, it seems to re-orient correctly.
Why does that happen? How can I fix it?

I find I have to set reset the frame of my main view for changing orientation:
CGRect mainViewFrame = CGRectMake(0.0, 0.0, 320, 480);
[self.view setFrame:mainViewFrame];

Related

Xib not scaling on iPhone 6

I have designed a xib whose configuration I have set as shown in the pic
I have added a UIImageView to the xib view and added proper constraints
So that the UIImageView appears full screen but the problem is that this works good for iPhone 5 but fails to scale on iPhone 6, I am not understanding the problem.
The UIImageView Configurations are as in the pic
To fill the UIImageView full screen, set the mode to Scale to fill. If it does not fill screen yet, then the size of image is not appropriate. i would recommend you to use vector graphics.
I was making a silly mistake while adding the UIView as subview I was not describing the frame
Describing the frame as,
[self.profileView setFrame:CGRectMake(0, 0, Get_Bounds.width, Get_Bounds.height - 64)];
This does the trick.

UIImageViews Jump around when a label changes - Xcode

I have falling UIImageViews falling on the screen and going back to the top in my game. I have another two image views on the bottom of the screen that their images change every so often. When those two bottom images change, the falling UIImageViews jump around the screen to their original position of when the game app opens, then they fall again. The same thing happens when the score label changes.
I have constraints on almost everything so im thinking that might have something to do with it but I don't know why.
Thank you very much for your help
OK so I'm sure you're aware of layout constraints and the NSLayoutConstraint class. (If not read this). Basically Apple uses these classes which are represented in the IB to position and resize subviews e.t.c.
Because of this you need to adjust the layout constraints of any view you want to move. To animate a UIView it used to be a case of changing the frame property and then getting the superview to redraw it's subviews; however, now you need to change the constant property of a view's layout constraints and then call layoutIfNeeded on the superview like so:
myImageViewConstraint.constant += 100;
[UIView animateWithDuration:1 animations:^{
[superview layoutIfNeeded];
}];
Then because you have changed the constraint, the view won't jump around.
Note: you should research this before jumping on Stack Overflow because this is readily available in the Apple Developer Resources.

UIScrollView doesn't work well with IB

I have a screen full of different UI objects that I need to scroll if on a 3.5 inch iPhone screen. I've added the ScrollView and setEnabled to YES but it still doesn't work. I'm thinking I should just code all the objects instead of using the IB and then add them to the scrollview. If I use a view with a scrollview on top with a view on top of that, then add them all to the subview, they lose their positioning. As for the constraints on the IB, there are simply too many objects and with hours spent on different constraint use, that will not be an option. Thanks for any suggestions and help.
Try to set the content size to be the size your frame, which is this case your view controller since you mentioned its "a screen full of different UI objects"
self.scrollView.contentSize = self.scrollView.frame.size;
then set scrollView frame to be the size of the view's frame
self.scrollView.frame = self.view.frame;
UIScrollView wont scroll, if the contents inside of the UIScrollview, are small enough to view without scrolling.
So if you have a UIView obj inside of your UIScrollView and that UIView is not Bigger than the UIScrollView, by default the UIScrollview will not scroll.

Autorotate: View height modified after return to original rotation

Pretty self explanatory, see this image:
Steps to reproduce:
"Before" marks the view position in portrait orientation prior to autorotation.
Rotate the device to landscape.
Rotate back to portrait. "After" shows the view position after this return to portrait.
It appears that the height of the container view has been increased. Has anyone experienced this before? I'm sure there is some obscure setting that is slightly off.
Are you sure you've set auto resizing masks correctly? Maybe that's the issue
You say in the comments that this is happening on the iPhone 5. I had some problems positioning views on the iPhone 5, and these issues could not be fixed with autoResizingMask no matter what setting I tried. I eventually solved the problem by adding these two lines of code in viewDidLoad:
UIScreen *screen = [UIScreen mainScreen];
[self.view setFrame:[screen applicationFrame]];
Since your problem is on a rotation, you probably need these lines to be executed upon rotation rather than in viewDidLoad.

xcode 4.5, iPhone 5 breaks my UIScrollView

So I've got a pretty complex project. I'm using both interface builder and xcode directly to build objects. Right now I have UIScrollViews being built in IB, where they need to be, and UIButtons built on top of those scrollviews. There are several scrollviews in the same spot, but that really shouldn't make much of a difference.
Anyway, the issue is that it works perfectly on the iPhone 4. But when building on the iPhone 5, it moves the Scrollviews to the bottom of the screen, where before it was x=0, y=361. All my other objects are being placed correctly with some empty space underneath them. I know how to check for iPhone 5:
I don't know how to post code on here with colors and whatnot, they make it super complicated so here is how I'll do the if/then:
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480){
// iPhone Classic
}
if(result.height == 568){
// iPhone 5
}
}
I don't know of a way to do if/then in IB. I tried just manually changing the location this way:
[peopleScrollView scrollRectToVisible:CGRectMake(0, 449, 320, 58) animated:NO];
That did not work. So, what I'm asking is there a way to change the location of a UIScrollView in the code itself? If there is not, then I think I will have to build all 5 UIScrollViews manually in code, which I definitely do not want to do.
If you select your Scroll View object, then click the Size Inspector module, you will notice the default Autosize Mask is set to: Left, Top.
Depending on your view "Mode" option, and your view's "Resize Subviews Automatically" option, this view and subviews will be shifted down on the 4" screen compared to the 3.5" screen.
Depending on what your particular view should look on each screen is up to you. On my project, I adjust the autosize mask to Left, Top, and Bottom,
as I want my UIScrollView and subviews to remain at the top of the screen (as drawn in IB) on the 3.5" and 4".
You can also set vertical and / or horizontal sizing arrows inside the box in the autosize graphic
. This will attempt to scale object as accordingly for dynamically sized screens.
The Autosize Mask should be your new best friend with iPhone5.
See Xcode Interface Builder. How Do These Autosizing Mask Settings Differ? for more info.