iOS 7 UITableView didSelectRowAtIndexPath pushView

2019-04-20 05:54发布

问题:

EDIT: I have Found the answer to my own question. Please look at the bottom of my post.

I am having an animation issue trying to push a UIViewController in didSelectRowAtIndexPath on a UITableView programmatically. When I run this code in iOS 6 it works fine. In iOS 7 the animation is messed up (it animates to the left about 20% of the screen then disappears). If I do this in storyboard the animation is fine. Am I missing something or is there a way around this without using storyboard?

// This is a cut down example.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIViewController *viewController = [[UIViewController alloc] init];
    [self.navigationController pushViewController:viewController animated:YES];
}

This is what it looks like right before the tableView disappears. I understand why it does this. Its because the new view being pushed should animate sliding in over it as it does when using storyboard. For some reason it does not work programmatically.

EDIT: This is all I missed For some reason you have to set a backgroundColor.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIViewController *viewController = [[UIViewController alloc] init];
    [viewController.view setBackgroundColor:[UIColor orangeColor]];
    [self.navigationController pushViewController:viewController animated:YES];
}

回答1:

This is the default "parallax" behavior triggered by the pushViewController:animated: method in iOS7.

Having little experience with storyboards I suspect that their segues are different from UINavigationController push/pop animations.

You can override the default push/pop by building custom animated or custom interactive transitions.

Or you could use the UIViewController method:

transitionFromViewController:toViewController:duration:options:animations:completion:

to customize this behavior. Hope this helps and hope I've understood your question...



回答2:

I've been have similar problems which I managed to fix by delaying the call

[self performSelector:@selector(displayMyViewController) withObject:nil afterDelay:0.2f];


回答3:

I have experienced this issue too. Another reason for this would be modifying the destination view's alpha. At least that's what i did.

If you want to change the view's opacity when you're displaying a loading HUD or something, don't modify the view's alpha. Instead, adjust the alpha of the Table View or Collection view, or whatever you have in that view. If you have several elements, put them all in a view and change that view's alpha.

Another reason would also be the background color, as stated above. You need to set the background color of the destination view. I ran into this several times as well.