Autorotate: View height modified after return to original rotation - objective-c

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.

Related

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.

UIButtons titleLabel clips Text after being rotated by CGAffineTransformMakeRotate()

I am developing a iOS-6 app. I have a UIViewController with a view that needs fixed orientation (portrait mode). But when the phone is rotated, one control on that view needs to be moved and rotated (so that it will always be in the upper left corner, and its text will be readable).
I am achieving this by shifting the control(a UIView) using the frame-property of my control (it is a custom view, more on that later), and then using CGAffineTRansformMakeRotate() afterwards, since I know that it's not advisable to use the frame after rotating a view. Everything is fine so far, but here's the thing: That custom view has three UIButtons of type UIButtonTypeCustom as its subviews. Because I rotated the View, but cannot rotate the buttons inside the view (they are not squares), I need to rotate the titleLabels of the Buttons for the text to be readable in the new deviceOrientation.
But it won't work very well. The text will be rotated, as I intended, but it will be clipped by the titleLabel, because the titleLabel has the wrong frame. I checked this by applying borders to the label. So I need to change the titleLabels frame, right? But how can I do that? I tried setting it using [titleLabel setFrame: frameThatFits];, but to no avail. (frameThatFits is a CGRect I created). Also, calling [button.titleLabel sizeToFit]; has no effect that I could see.
I am using [button setTitle:title forControlState: UIControlStateNormal];to set the title.
TL;DR: I'm trying to change the frame/bounds of a UIButtons titleLabel after rotating it using an affine transformation. Any help?
Thanks.
PS: I can supply code when needed, but I wouldn't know what to show you. Tell me what you need, I'll post it.
OK, first of all, thanks to everyone who tried to help. Im posting an alternative solution for my problem, and although it doesnt really address the problem of changing the titleLabels dimensions, it will result in the proper display of my ViewController.
It turns out using the frame is a bad idea. I initially used the frame to reposition the view and i figured that this couldnt be a problem because i only ever applied transformations afterwards, but i was wrong. Because OBVIOUSLY i tried to change the titleLabels frame. AFTER the rotation. And that didnt work.
So the way to go here is using the center-property and the bouds of the view consistently throughout the code. It will result in properly rotated Buttons, that do not need any fidgeting afterwards.
My takeaway here is that i will never ever again use the frame-property outside of a NSLog-statement. But why [button sizeToFit];wouldnt yield any results is still beyond me. If i ever figure it out, i might post it if i remember.
EDIT:
#ZevEisenberg nailed it with this comment:
“Warning: If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored.” So you are right to use the center and bounds here, but if you do not have a transform, the frame is perfectly safe to use.
NEXT EDIT:
Heres how i ended up repositioning the Buttons:
-(CGPoint)centerForView:(UIView *)view{
//calculate a suitableposition for the view
//depending on the current orientation and the device type (iphone 4S/5, etc)
return point;
}
Then, as a reaction to the deviceOrientation change notification, i apply CGAffineTransformIdentity to all the views, reposition them using my centerForView shown above, and apply the correct rotation transformation to the View. I do this for all the subviews every time the divice rotates, like so:
-(void)setRightRotationTransformations{
[self resetAllTransformations];
self.someSubview.transform = CGAffineTransformRotate(self.someSubview.transform, -M_PI_2);
}
In my case works such hack:
set Line Break mode to Word Wrap
Add extra line to title (even for one line title)

IKImageView resize is blocky

I am putting an image into an IKImageView, and immediately sizing it to fit. Whenever I do this, the image originally appears at 1-1 size (huge) and then resizes down, which would be fine if the animation was smooth. However, the animation looks ... fluttery? There are big blocks, like 2 inches square, of the image that appear and shrink independently of each other. The effect is a little annoying, and almost to the level where it might give an epileptic a seizure... (I'm exaggerating a little).
Is this a bug in IKImageView? Is it a bug in the animation? Will it go away if I turn off the animation (How do I do that? setAnimates: NO doesn't seem to do anything, nor does overloading animates to return NO in my subclass...
EDIT: addedcode:
NSImage* image = [doc currentImage];
[imageView setImage: image];
[imageView zoomImageToFit: self];
This is in the app controller, so self is the application (or plugin, depending on which version I'm looking at)
If you tell the IKImageView to autoresize, then it won't animate the new image to the new size and the artefacts will not be a problem. The image will be sized to fit in the image view immediately.
imageView.autoresizes = YES;

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

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];

WebView renders in the wrong location in my ScreenSaverView

I've written a screen saver that displays a web page. It works exactly as I want it to on my main display, but in the preview and secondary displays, the web view is hanging off the top of the screen.
Example (from preview):
screen saver bug http://img.skitch.com/20081212-nk5cqrgfds1funr1a3p72aw25q.preview.jpgUploaded with plasq's Skitch!
My code is pretty straightforward. From within initWithFrame:isPreview: I have the following code:
webview = [[WebView alloc] initWithFrame:frame
frameName:#"main"
groupName:#"main"];
[self addSubview:webview];
Does anyone have any idea what's happening?
If anyone wants to play with the project, the code is on github.
I looked at your code and you're using the superview's (WebSaverView) frame rectangle as the WebView's frame. You should instead use it's bounding rectangle ([self bounds]). The bounding rectangle represents the area "inside" the superview. As you've seen, the two may not always have the same origin, which is causing this issue. You should not be adjusting the frame you're passed in initWithFrame:, since the origin probably has a very good reason for not being 0,0.
I think the frame you're giving the WebView is in the wrong coordinates. Remember that a view's frame is expressed in terms of the superview's coordinate system (bounds).
You should be making the view as big as the superview's -bounds, if I get correctly what you want to do: usually a rect from (0,0) to (width,height).