Position UIview when changing orientation

大兔子大兔子 提交于 2019-12-13 13:35:27

问题


I have a custom UIView videoView in a UIViewController in InterfaceBuilder.

When I load it, it looks fine, but when I turn it to say "to landscape from portrait" it doesn't go where the coordinates tell it. And then when I turn it back it's wrong again.

I have the following code:

-(void)viewWillAppear:(BOOL)animated
{
    [self processRotation:self.interfaceOrientation];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}    
-(void)processRotation:(UIInterfaceOrientation)_interfaceOrientation
{
    if (_interfaceOrientation == UIInterfaceOrientationLandscapeLeft || _interfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        videoView.frame = CGRectMake(20.0f, 77.0f, 984.0f, 595.0f);
    }   

else if (_interfaceOrientation == UIInterfaceOrientationPortrait || _interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        videoView.frame = CGRectMake(20.0f, 297.0f, 728.0f, 453.0f);
    }
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [self processRotation:toInterfaceOrientation];
}

回答1:


When the view rotates, make sure you are going from portrait to landscape:

if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation) && UIInterfaceOrientationIsLandscape(interfaceOrientation)

Because the orientation has not changed yet, instead of

videoView.frame = CGRectMake(20.0f, 77.0f, 984.0f, 595.0f);

write:

videoView.frame = CGRectMake(77.0f, 22.0f, 595.0f, 984.0f);

Alternatively, you can go into Interface Builder and change the autoresizing masks there and don't do anything in the code. To change the autoresizing masks go to the Size inspector in the right pane (ruler icon). You will see a square with a vertical and horizontal arrow inside, and a letter I shaped line coming out of each side. The arrows inside, when red (turned on, click to turn on), will make the object stretch in that direction automatically when the view resizes. Turning on an I shaped line essentially docks the view to that side, meaning that the distance between the side of the view and that side will always remain the same. e.g. If you have a toolbar at the top which gets thinner in landscape do not select the top line, or your view will end up miles away from the top toolbar.

Or you can return no for autorotation and implement it yourself, by calling

self.interfaceOrientation = toInterfaceOrientation;

and then manually changing the coordinates like in your original code.

You really need to experiment with it, I run into this issue all the time and I fix it differently each time.




回答2:


You are using willRotateToInterfaceOrientation:, which is called before the rotation happens. If the videoView has it's autoresizingMask set, then the autoresizing will kick in later and change the position of your view.

You should normally be doing this in - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration and ensure, that the autoresizingMask of your videoView is set to UIViewAutoresizingNone



来源:https://stackoverflow.com/questions/6621616/position-uiview-when-changing-orientation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!