From 272803ecab89e383a06c2f72a1aef7af35b9bdf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Gra=CC=88tzer?= Date: Sat, 14 Jul 2012 18:20:22 +0200 Subject: [PATCH] Some bugs fixed --- Demo/SGTabs/SGAppDelegate.m | 2 +- Demo/SGTabs/SGViewController.m | 27 +++++++++- Source/SGTabView.m | 8 +-- Source/SGTabsViewController.m | 92 ++++++++++++++-------------------- 4 files changed, 70 insertions(+), 59 deletions(-) diff --git a/Demo/SGTabs/SGAppDelegate.m b/Demo/SGTabs/SGAppDelegate.m index 9697c4d..2c22d59 100644 --- a/Demo/SGTabs/SGAppDelegate.m +++ b/Demo/SGTabs/SGAppDelegate.m @@ -54,6 +54,6 @@ - (void)openTab { } - (BOOL)canRemoveTab:(UIViewController *)viewController { - return self.tabController.count > 1; + return YES;//self.tabController.count > 1; } @end diff --git a/Demo/SGTabs/SGViewController.m b/Demo/SGTabs/SGViewController.m index 71c77cd..92b066a 100644 --- a/Demo/SGTabs/SGViewController.m +++ b/Demo/SGTabs/SGViewController.m @@ -34,7 +34,8 @@ - (void)viewDidLoad [super viewDidLoad]; self.webView.delegate = self; - self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 300.0, 25.0)]; + self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 400.0, 25.0)]; + self.textField.autoresizingMask = UIViewAutoresizingFlexibleWidth; self.textField.backgroundColor = [UIColor whiteColor]; self.textField.text = @"http://www.google.com"; self.textField.clearButtonMode = UITextFieldViewModeAlways; @@ -67,6 +68,30 @@ - (void)viewDidUnload [super viewDidUnload]; } +- (void)viewWillAppear:(BOOL)animated { +#ifdef DEBUG + NSLog(@"%s", __FUNCTION__); +#endif +} + +- (void)viewDidAppear:(BOOL)animated { +#ifdef DEBUG + NSLog(@"%s", __FUNCTION__); +#endif +} + +- (void)viewWillDisappear:(BOOL)animated { +#ifdef DEBUG + NSLog(@"%s", __FUNCTION__); +#endif +} + +- (void)viewDidDisappear:(BOOL)animated { +#ifdef DEBUG + NSLog(@"%s", __FUNCTION__); +#endif +} + - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { diff --git a/Source/SGTabView.m b/Source/SGTabView.m index 3b457d6..2701626 100644 --- a/Source/SGTabView.m +++ b/Source/SGTabView.m @@ -45,7 +45,7 @@ - (id)initWithFrame:(CGRect)frame title:(NSString *)title _cap = kCornerRadius/frame.size.width; self.contentStretch = CGRectMake(_cap, 0.0, 1.0, 1-_cap); - + self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; self.titleLabel.textAlignment = UITextAlignmentCenter; self.titleLabel.lineBreakMode = UILineBreakModeTailTruncation; @@ -78,9 +78,9 @@ - (void)layoutSubviews { if (t.width > b.size.width*0.75) { t.width = b.size.width*0.75 - 2*margin; } - + if(!self.closeButton.hidden) { - self.titleLabel.frame = CGRectMake((b.size.width - t.width)/2 - margin, + self.titleLabel.frame = CGRectMake((b.size.width - t.width)/2 + margin, (b.size.height - t.height)/2, t.width, t.height); } else { @@ -89,7 +89,7 @@ - (void)layoutSubviews { t.width, t.height); } - self.closeButton.frame = CGRectMake(b.size.width - 2*margin - 25, 0, 25, b.size.height); + self.closeButton.frame = CGRectMake(2*margin, 0, 25, b.size.height); } - (void)setTitle:(NSString *)title { diff --git a/Source/SGTabsViewController.m b/Source/SGTabsViewController.m index 31e3c3f..4f4f629 100644 --- a/Source/SGTabsViewController.m +++ b/Source/SGTabsViewController.m @@ -120,27 +120,30 @@ - (void)addTab:(UIViewController *)viewController { options:NSKeyValueObservingOptionNew context:NULL]; + if (!self.currentViewController) { + [self.tabsView addTab:viewController.title]; + [self.view addSubview:viewController.view]; + _currentViewController = viewController; + [viewController didMoveToParentViewController:self]; + if (_toobarVisible) + [self.toolbar setItems:self.currentViewController.toolbarItems animated:YES]; + + return; + } - [self.currentViewController viewWillDisappear:YES]; - // Add tab selects automatically the new tab - [UIView transitionWithView:self.view - duration:kAddTabDuration - options:UIViewAnimationOptionAllowAnimatedContent - animations:^{ - [self.tabsView addTab:viewController.title]; - if (self.currentViewController) { - [self.currentViewController.view removeFromSuperview]; - } - [self.view addSubview:viewController.view]; - } - completion:^(BOOL finished){ - [self.currentViewController viewDidDisappear:YES]; - _currentViewController = viewController; - - if (_toobarVisible) - [self.toolbar setItems:self.currentViewController.toolbarItems animated:YES]; - [viewController didMoveToParentViewController:self]; - }]; + if (_toobarVisible) + [self.toolbar setItems:viewController.toolbarItems animated:YES]; + [self transitionFromViewController:self.currentViewController + toViewController:viewController + duration:kAddTabDuration + options:UIViewAnimationOptionAllowAnimatedContent + animations:^{ + [self.tabsView addTab:viewController.title]; + } + completion:^(BOOL finished){ + _currentViewController = viewController; + [viewController didMoveToParentViewController:self]; + }]; } } @@ -201,19 +204,10 @@ - (void)removeViewController:(UIViewController *)viewController index:(NSUIntege if (self.tabContents.count == 0) {//View controller was the last one [viewController willMoveToParentViewController:nil]; _currentViewController = nil; - [viewController viewWillDisappear:NO]; - [UIView transitionWithView:self.tabsView - duration:kRemoveTabDuration - options:UIViewAnimationOptionAllowAnimatedContent - animations:^{ - [viewController.view removeFromSuperview]; - [self.tabsView removeTab:index]; - [self.toolbar setItems:nil animated:NO]; - } - completion:^(BOOL finished){ - [viewController viewDidDisappear:NO]; - [viewController removeFromParentViewController]; - }]; + [viewController.view removeFromSuperview]; + [self.tabsView removeTab:index]; + [self.toolbar setItems:nil animated:YES]; + [viewController removeFromParentViewController]; return; } else if (oldIndex >= self.tabContents.count) { index = self.tabContents.count-1; @@ -224,26 +218,18 @@ - (void)removeViewController:(UIViewController *)viewController index:(NSUIntege if (_toobarVisible) [self.toolbar setItems:to.toolbarItems animated:YES]; - [viewController willMoveToParentViewController:nil]; - [viewController viewWillDisappear:YES]; - [UIView transitionWithView:self.view - duration:kRemoveTabDuration - options:UIViewAnimationOptionAllowAnimatedContent - animations:^{ - [self.tabsView removeTab:oldIndex]; - - if (self.currentViewController == viewController) { - [viewController.view removeFromSuperview]; - self.tabsView.selected = index; - [self.view addSubview:to.view]; - } - } - completion:^(BOOL finished){ - [viewController viewDidDisappear:YES]; - [viewController removeFromParentViewController]; - _currentViewController = to; - }]; - + [self transitionFromViewController:viewController + toViewController:to + duration:kRemoveTabDuration + options:UIViewAnimationOptionAllowAnimatedContent + animations:^{ + [self.tabsView removeTab:oldIndex]; + self.tabsView.selected = index; + } + completion:^(BOOL finished){ + [viewController removeFromParentViewController]; + _currentViewController = to; + }]; } - (void)removeViewController:(UIViewController *)viewController {