UINavigationController is loading the image incorrectly due to orientation / shaking

Reference Information. The app has a shake to return home. Home browsing Supports portrait only. If you shake a little harder than usual, then the view you click starts to rotate (which is good), but then it detects a shiver and makes the popViewControlller for home viewing. When he does this, he just loads the navigation controller, but the view under (home content) loads behind the panel and stretches (it basically loads under the navigation panel, so it stretches).

The back button handles this just differently from landscape to portrait (since its not average transitions)

How do I handle this change of orientation (from shaking) so that I can return to the root view controller without loading the view under the navigation bar?

Edit: what happens, the content thinks it has the whole view for downloading, so it stretches to take the whole screen, not realizing that there is a navigation bar above it. I can tell since image loading is stretching

50 bounty added.

Edit here How I Detect Shakes and Popping

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { if ( event.subtype == UIEventSubtypeMotionShake ) { UINavigationController *navController = self.navigationController; [[self retain] autorelease]; HomeViewController *home = [[HomeViewController alloc]init]; [navController popViewControllerAnimated:YES]; home.title =@ "Home View Controller"; [home release]; } if ( [super respondsToSelector:@selector(motionEnded:withEvent:)] ) [super motionEnded:motion withEvent:event]; } 

Here is my application delegate:

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { navController = [[UINavigationController alloc]init]; [self.window addSubview:navController.view]; HomeViewController *home = [[HomeViewController alloc]init]; [[self home] setFrame:[[UIScreen mainScreen] applicationFrame]]; 

I will include the layout here.

Normal view:

Normal view

Stretched appearance after shaking / pop:

Streched

 - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { } - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {} - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return YES; } 
+6
source share
4 answers

I am a little puzzled by your code, so I really suggest starting from the beginning. As Lucia noted, there is no reason to recreate the HomeViewController. I am also puzzled by the [[self self]] abstract]; a little. This is not necessary if you are not doing something wrong elsewhere.

So, I would start with this ... In the app: didFinishLaunchingWithOptions: do something like this:

  - (BOOL) application: (UIApplication *) application didFinishLaunchingWithOptions: (NSDictionary *) launchOptions 
     {
         HomeViewController * home = [[[[HomeViewController alloc] init] autorelease];
         UINavigationController * navController = [[[[UINavigationController alloc] initWithRootViewController: home] autorelease];
         [self.window addSubview: navController.view];
     }

Your navigation controller will be saved in the window, and the navigation controller will save your HomeViewController.

Then in motion Ended: withEvent: do something like:

  - (void) motionEnded: (UIEventSubtype) motion withEvent: (UIEvent *) event
     {
         if (event.subtype == UIEventSubtypeMotionShake)
         {
             [self.navigationController popViewControllerAnimated: YES];
         }
     }

It should be.

If this does not work, can you give any other information? For example, does HomeViewController (and return YES) implement in shouldAutorotateToInterfaceOrientation :? If so, can you return no, so it does not rotate, since your first line reads: "Home browsing only supports portrait"?

Edit: example willRotateToInterfaceOrientation:duration: and didRotateFromInterfaceOrientation:

In the header for any controller, you detect jolts by adding a boolean:

     BOOL isRotating;

In your implementation file, add two UIViewController methods that we want to override, for example:

     - (void) willRotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation duration: (NSTimeInterval) duration {
         [super willRotateToInterfaceOrientation: toInterfaceOrientation duration: duration];
         isRotating = YES;
     }

     - (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation {
         [super didRotateFromInterfaceOrientation: fromInterfaceOrientation];
         isRotating = NO;
     }

Now do something similar for the event handler:

  - (void) motionEnded: (UIEventSubtype) motion withEvent: (UIEvent *) event
     {
         if (event.subtype == UIEventSubtypeMotionShake &&! isRotating)
         {
             [self.navigationController popViewControllerAnimated: YES];
         }
     }
+2
source

You tried to call [[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationPortrait animated:YES]; to your home controller? You can also try to place this where you find shaking.

+2
source

in your home xib controller, go to inspector for presentation and set the top bar as a navigation bar .. and in the field of view the set self.navigationBarHidden = NO; ...

Note: there is much that is wrong with the code you posted. But not one of them causes an orientation problem ... in fact, this is apparently the only code you need in this method:

 - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { if (event.subtype == UIEventSubtypeMotionShake) { [navController popViewControllerAnimated:YES]; } } 

so that you can change this code.

+2
source

I ran into this problem when selecting a navigation bar. I'm not sure what causes it, but you can get around it by calling

 [[self loadingView] setFrame:[[UIScreen mainScreen] applicationFrame]]; 

after the representation of the problem is added to the window in the application deletion.

+2
source

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


All Articles