0
[self addChildViewController:self.redVC];
[self.view addSubview:self.redVC.view];
[self.redVC didMoveToParentViewController:self];

"self" is a tabBarController, "redVC" is a test VC. I want full life cycle(such as viewDidAppear and rotation chain) to pass to childViewController(redVC), so I use childVC. However, this method will show the tabBar.

When just put self.redVC.view above the self.view, not by addChildViewController. The tabBar can be covered. Overlay a view over whole screen, when using UITabBarController?.

Meanwhile, I may add a transparent view to tabBarViewController's view, and in this situation, the tabBar should be seen. Thus, I don't want to change the tabBar's frame or to hide it.

Is there a method to achieve them all?

  • Full life cycle pass and rotation chain from the parent viewController
  • Full screen view to add(if this view is transparent, the toolBar should be seen)
  • Don't change or hide tabBar
Community
  • 1
  • 1
Jeff Ma
  • 31
  • 2
  • 9
  • You firstly write: "However, this method will show the tabBar." and then "Don't change or hide tabBar" -> so do you want to have tabbar visible or not? – Nat Sep 01 '15 at 07:31
  • @Vive Sorry for my poor English, I've added three conditions. I want to have tabBar visible. – Jeff Ma Sep 01 '15 at 07:34
  • Then why won't you just pushViewController..? – Nat Sep 01 '15 at 07:34
  • I've edited my answer to show how to push that view and keep the tabBar visible. – Nat Sep 01 '15 at 07:40

1 Answers1

0

Answer if you want to have tabbar INVISIBLE:

Instead of such hacks, you should just hide the tabbar on viewWill(Did)Appear and viewDid(Will)Disappear. Add your viewController to normal stack.

You can also just add modalViewController.

PushViewController *pushViewController = [[PushViewController alloc] init];
[self presentViewController:pushViewController animated:YES completion:^{}];

This way, your PushViewController will have all view lifecycle methods (like viewDidAppear: etc) and you tabbar won't be visible when this controller is visible.


Answer if you want to have tabbar VISIBLE:

In your storyboard (or code) add UINavigationBar.

[self.navigationController pushViewController:pushViewController animated:YES];

BTW: Try to avoid such hacks. They may not work in the future, are not friendly to read and generally tends to show that code isn't good.

You should also reread the section about MVC, view lifecycle and UIViewController in Apple documentation.

Nat
  • 12,032
  • 9
  • 56
  • 103
  • I edit my question and explain why i don't hide or change the frame of tabBar. – Jeff Ma Sep 01 '15 at 07:22
  • @JeffMa Your approach is wrong. `viewDidAppear:` etc are `UIViewController` methods and you're trying to add a `UIView` to `self.view` -> it is a different object, nothing strange `UIView` doesn't handle `UIViewController` methods. Anyway. Easiest way will be to present a new `UIViewController` with your view modally. – Nat Sep 01 '15 at 07:28
  • presentViewController will replace the parent with child. So when the child's view is transparent, the parent's view is invisible. – Jeff Ma Sep 01 '15 at 07:40
  • @JeffMa It won't replace, it will push new one to the stack and move to that new one. If it is transparent, it won't be visible. However your question also earlier didn't say anything about transparency. – Nat Sep 01 '15 at 07:42
  • Maybe I should add some background information about why i want this redVC. The redVC is some universal pop layer of the whole app. When it receive some notifications, it will pop out. My implementation is to add it as a childViewController to the rootViewController. In some condition, it will be transparent and I rewrite pointInside method to pass the point to the parentVC. So push to a navigation stack may not be a good choice. – Jeff Ma Sep 01 '15 at 08:14
  • To present such data you can use ready components like TSMessages (https://github.com/KrauseFx/TSMessages), which reacts to rotation changes etc. Adding to self.view anything (even childViewController) won't change self.view size, so if self.view isn't fullscreen it won't magically happen to be. You should separate implementation of that notification from your view controllers. You may add it to your window, surely not tabbar. The whole topic is too broad and there is too much to write to explain it correctly. You may just take a look into similar things implementation at cocoacontrols. – Nat Sep 01 '15 at 08:28
  • Thanks for patience. I used TSMessages before and already read some of its code. Actually, I do have another requirement. This redVC should have the ability to force the whole app into only one rotation, implemented by writing `supportedOrientation` method. That's the main reason why I insist to use a childVC. In production environment, the redVC can load a webView and get data from a web API to determine the orientation of the device to show the webView etc. I used the "redVC" SDK in some apps to give them ability to do so until met an app which has a tabBarVC as its rootVC. – Jeff Ma Sep 01 '15 at 08:47
  • @JeffMa This is a very wrong user experience. Generally if your app handles multiple rotations, the users expects it handle it everywhere not under special conditions (exception: videos). It's your choice. If you want implement it anyway, I'd use a ready library, and the easiest (but not prettiest) solution is to set a `BOOL` flag in your `UIViewController` when that control appears. Then implement rotation methods and return rotating/nonrotating results basing on that `BOOL`. You can use inheritance here. There are other options, but they're too complex. – Nat Sep 01 '15 at 08:57
  • The pop webView does show some videos or simple html5 games. They are designed in landscape mode. When this "pop" childVC is removed from its parent, I'll use a hack method to force system fresh current rotation so the app's orientation will restore. I try to do this RedVC as a external SDK, so I don't want to injection original code or system VC implementation. As for forcing a specific rotation, after long long google, i fount the only correct way is using `supportedOrientation`. – Jeff Ma Sep 01 '15 at 09:13
  • Maybe the ugly but effective way is to tell the developers of this "tabBarVC" app to use a containerVC as rootViewController, rather than tabBarController. – Jeff Ma Sep 01 '15 at 09:18