简体   繁体   中英

UIView Components Not Moving With View

I have created a UIView in a storyboard. See here for more information. My issue is when the user "logs in" then I present a new view bringing it from the left side of a screen and the main view slides off to the left, like a push navigation, but all programmatically.

So I have decided to animate this "navigation" using the following code. view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; view.backgroundColor = [UIColor whiteColor];

//add stuff to view
[view addSubview: label];

view.frame = CGRectMake(320, 0, 0, 480);
view.clipsToBounds = YES;

[self.view addSubview:view];

//show view
[UIView animateWithDuration:.5
                 animations:^{
                     self.view.frame = CGRectMake(0,0,0,480); //move self.view off left
                     view.frame = CGRectMake(0, 0, 320, 480); //move new view in place
                 }];

My view is being successfully displayed but the existing view, self.view is not going off the left side of the screen like I want it. It moves, but it doesn't take it's components with it... When I comment out //view.frame - CGRectMake(0,0,320,480); inside the animation, so I don't display the new view I get this in the simulator on login.

The white background of the view leaves the screen but the components do not. This causes an issue because when I put the two together it does not look like the "new view" is replacing the old view like a navigation push controller but instead sliding over it. Is there a way to move these components with the self.view when I move it off the screen

Thanks for the help!

I am not sure if you can move self.view . Even if it is possible, it's not a good idea...

Anyway your code

self.view.frame = CGRectMake(0,0,0,480);

doesn't slide the view. It scales the view into a zero-width rectangle, which explain why its subviews are still there. By default subviews are aligned to top-left corner of their superviews, and since self.view 's top-left hasn't moved (still (0, 0)), they remain the same. The proper way to slide a view off the screen is:

self.view.frame = CGRectMake(-320,0,320,480); // width & height MUST remain unchanged

Still, this will NOT work, since you have made view a subview of self.view ... which means this line:

view.frame = CGRectMake(0, 0, 320, 480);

will set view.frame relative to self.view which now has been slided off the screen! So they will be both gone. Another good reason why you shouldn't play with self.view.frame .

IMHO, you should create a view v1 as the main view, and v2 the view that appears when logged in. Make them both subviews of self.view , hide v2 behind v1 and when user logs in, slide v1 off the screen as described above. Or maybe put v1 to the right of v2, and slide them both (so v1 will be sliding out why v2 sliding in, similar to a navigation controller).

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