开发者

Cannot properly rotate a translated UIView on the iPad

I have an UIViewController that has two UIViews inside, whose layout (set in Interface Builder) is something like this (the magenta colored view is the UIViewController default view):

Cannot properly rotate a translated UIView on the iPad

and I added the main view to a root view obtaining this effect (by not showing the darker view placing its frame at the bottom):

Cannot properly rotate a translated UIView on the iPad

Now when the device rotates, what I obtain is this:

Cannot properly rotate a translated UIView on the iPad

whereas I'd l开发者_运维知识库ike to still hide the darker view. This is the code I use to hide it when it appears (I use it in the viewDidLoad method):

- (void)hideAll:(float)time{
        CGRect hiddenFrame = CGRectMake(0, 1024, self.view.frame.size.width, self.view.frame.size.height);
        [self.view setFrame:hiddenFrame];
        [self.view setNeedsDisplay];
}

and it appears to work, but when I call a variant of it when rotating (in willRotateToInterfaceOrientation:duration:), nothing happens:

    CGRect hiddenFrame = CGRectMake(0, 748, self.view.frame.size.width, self.view.frame.size.height);
    [self.view setFrame:hiddenFrame];
    [self.view setNeedsDisplay];

What am I missing? have I to set some other view property in Interface Builder?

UPDATE

It seems to me that the coordinate system does not change after the device has been rotated (ie the origin set in the upper-left corner in portrait mode will become the upper-right after a clockwise rotation in landscape mode)


Check your struts and springs in IB. It maybe that when you autorotate, the strut fixing you to the top of the screen moves your frame back to the top.

Try adding your code to the didRotateToInterfaceOrientation method so it can run after rotation has occurred.


Why to you change the frame to hide it, and not just

[self.view setHidden:YES];

second thing is that you should try to set the "Autoresizing" panel to handle the orientation changes, and not by changing the frame of the view.

hope i understood you right.

shani


The problem I encountered can be faced in two ways (I hope this can save some time for someone in the future):

  • using an UIViewcontroller (loaded from xib) and only its view as the rotating view, that is my original attempt to solve it. In this case you have to programmatically set the frame for the view when you want to hide it, and by doing so you have to consider that the coordinate system for the controller remains untouched and does not rotate (ie. it is always originated from the same corner)
  • using a UIView (loaded from xib) inside an already existing UIViewcontroller, in this way the view will automatically rotate and resize and you will not have to take the proper coordinates value into account at each rotation (this is by far the simplest way)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜