简体   繁体   中英

Move a UIView's superview by setting its center property

I have a very strange problem. I trying to rotate and move a UIView's superview manually in viewDidAppear like so:

- (void)viewWillAppear:(BOOL)animated
{
    // ...

    [self.view.superview setTransform:CGAffineTransformMakeRotation(M_PI/2)];
    [self.view.superview setCenter:CGPointMake(100, 200)]; // (100, 200) is toy value

    // ...
}

For some reason, the rotation is applied correctly but the superview's center (ie position) is not moved at all. I've also tried setting the transform to a CGAffineTransform that's a combination of a rotation + translation (or just a translation alone), and that won't move the superview either.

If it makes a difference, self.view.superview is the top-most view (ie self.view.superview.superview is nil ).

Am I missing something very simple here?

Edit: nielsbot's comment was correct in that putting it in viewDidLoad worked, but by then, the view has already appeared so there's a flicker where it snaps to the new location. Is there a way around this?

Try setting the bounds of the view so:

[self.view.superview setBounds:CGRectMake(x,y,self.view.superview.width,self.view.superview.height)];

This should move your view to the x,y position you give him But of course this is not the center

The center just represents a special point that is for example used for rotations

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM