Why are my routines not properly centered depending on the orientation of the device?

I'm trying to create a programmable view controller with subviews that span the screen. I succeeded, and when I get to this view, the centered view appears in the center and rotates correctly when you rotate the device.

But in this navigation application, if I introduced a custom view when not in portrait mode, then the view that needs to be focused will position itself in a place that is not the center of the screen. I put all the necessary autoresize properties on the views, controller, parent, grandmother, holy virgin ... and I ran out of things to stick on the autoresist mask, and yet the example looks bad.

I am sure that I am missing a call to some magic method that will fix everything, but I did not understand how to call or where (setNeedsDisplay? SetNeedsLayout?). To demonstrate this problem, I created a complete example, available at https://github.com/gradha/iPhone-centered-rotation-test , which you can clone and run in a simulator or device. I created this from Apple's navigation controller by simply adding a fake cell that nudges the view I create manually.

The custom view can be found at https://github.com/gradha/iPhone-centered-rotation-test/blob/master/Classes/CenteredViewController.m , and here is the loadView method responsible for creating centered subzones:

/* Try to force the parent view to be as we want it. */ self.view.backgroundColor = [UIColor yellowColor]; self.view.autoresizesSubviews = YES; self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; /* Top left blue square, for reference. */ blue_ = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 130, 130)]; [self.view addSubview:blue_]; blue_.backgroundColor = [UIColor blueColor]; [blue_ release]; /* Create red centered rectangle. */ red_ = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]; red_.backgroundColor = [UIColor redColor]; red_.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; red_.center = self.view.center; [self.view addSubview:red_]; [red_ release]; 

Here is a link to a step-by-step guide on the application screen , first entering portrait mode and screen rotation (OK), then input in landscape mode and rotation (depending on the side of rotation, it looks pretty bad). When you enter a view in landscape mode, depending on the orientation, the red square will have an upper left corner of 196x34 or 140x34, which is too different.

What am I missing to make these objects the focus of attention correctly when you enter a view in landscape mode and perform automatic rotation?

+4
source share
1 answer

It seems that in one of the landscape orientations in loadView , the origin of the controller is set to (0, 20), which confuses the rotation and autoresistance that occurs after adding a view to the screen. Oddly enough, it is (0, 0) in all other orientations.

To fix the problem, add this line after [super loadView] :

 self.view.frame = self.view.bounds; 

To clarify: since the beginning of the frame is not (0, 0), the concept of self.view.center is incorrect in the context in which it is used. The center property is the center of this view, visible in its supervision. What you really want is the center of the bounds view.

Resetting the view frame to the bounds of the view, you can effectively change the origin to (0, 0) and center can be used correctly.

+7
source

Source: https://habr.com/ru/post/1345211/


All Articles