At once we will specify, everything described is true for iOS 7 and less. About what has changed in iOS 8 - at the end of the article (nothing good, in fact).
@interface ViewController () @property (strong, nonatomic) UIButton *buttonL; @property (strong, nonatomic) UIButton *buttonR; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = @"root"; self.view.backgroundColor = [UIColor whiteColor]; self.buttonL = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 1.0f, 1.0f)]; self.buttonL.backgroundColor = [UIColor blueColor]; [self.buttonL setTitle:@"push vc #1" forState:UIControlStateNormal]; [self.buttonL addTarget:self action:@selector(pushViewControllerOne) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.buttonL]; self.buttonR = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 1.0f, 1.0f)]; self.buttonR.backgroundColor = [UIColor redColor]; [self.buttonR setTitle:@"push vc #2" forState:UIControlStateNormal]; [self.buttonR addTarget:self action:@selector(pushViewControllerTwo) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.buttonR]; } - (void) viewWillLayoutSubviews { CGFloat width = self.view.bounds.size.width /2; CGFloat height = self.view.bounds.size.height; [self.buttonL setFrame:CGRectMake(0.0f, 0.0f, width, height)]; [self.buttonR setFrame:CGRectMake(width, 0.0f, width, height)]; } - (void) pushViewControllerOne { UIViewController *vc1 = [UIViewController new]; vc1.navigationItem.title = @"#1"; vc1.view.backgroundColor = [UIColor whiteColor]; [self.navigationController pushViewController:vc1 animated:YES]; } - (void) pushViewControllerTwo { UIViewController *vc1 = [UIViewController new]; vc1.navigationItem.title = @"#2"; vc1.view.backgroundColor = [UIColor whiteColor]; [self.navigationController pushViewController:vc1 animated:YES]; } @end
nested push animation can result in corrupted navigation bar
Finishing up a navigation transition. Navigation Bar subview tree might get corrupted.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't add self as subview'
// // StackNavigationController.m // #import "StackNavigationController.h" @interface StackNavigationController () <UINavigationControllerDelegate> @property (nonatomic, assign) BOOL isTransitioning; @property (nonatomic, strong) NSMutableArray *tasks; @property (nonatomic, weak) id<UINavigationControllerDelegate> customDelegate; @end @implementation StackNavigationController -(void)viewDidLoad { [super viewDidLoad]; if (self.delegate) { self.customDelegate = self.delegate; } self.delegate = self; self.tasks = [NSMutableArray new]; } // we should save navController.delegate to another property because we need delegate // to prevent multiple push/pop bug -(void)setDelegate:(id<UINavigationControllerDelegate>)delegate { if (delegate == self) { [super setDelegate:delegate]; } else { self.customDelegate = delegate; } } - (void) pushViewController:(UIViewController *)viewController animated:(BOOL)animated { @synchronized(self.tasks) { if (self.isTransitioning) { void (^task)(void) = ^{ [self pushViewController:viewController animated:animated]; }; [self.tasks addObject:task]; } else { self.isTransitioning = YES; [super pushViewController:viewController animated:animated]; } } } - (void) runNextTask { @synchronized(self.tasks) { if (self.tasks.count) { void (^task)(void) = self.tasks[0]; [self.tasks removeObjectAtIndex:0]; task(); } } } #pragma mark UINavigationControllerDelegate -(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated { self.isTransitioning = NO; if ([self.customDelegate respondsToSelector:@selector(navigationController:didShowViewController:animated:)]) { [self.customDelegate navigationController:navigationController didShowViewController:viewController animated:animated]; } // black magic :) // if one of push/pop will be without animation - we should place this code to the end of runLoop to prevent bad behavior [self performSelector:@selector(runNextTask) withObject:nil afterDelay:0.0f]; } @end
In recent versions of iOS, the situation has improved slightly. If earlier in iOS 7 and less, the application crashed while simultaneously pressing two buttons, then now in iOS 8, this will require 3 buttons . But crash is inevitable anyway.
Source: https://habr.com/ru/post/246757/
All Articles