viewWillAppear does not run when using addSubView!

viewwillappear not called uinavigationcontroller
viewwillappear not called uitabbarcontroller
viewwillappear not called swift
viewwillappear not called ios 13
viewwillappear not called after dismiss modal
viewwillappear not called when coming from background
viewwillappear not called tabbar
view will appear is not getting called in ios 13

I'm stuck! I can't see why viewWillAppear doesn't run in my code but viewDidLoad runs. If I understand it correctly viewDidLoad runs once on the first instance and viewWillAppear runs every time a view is added to the stack of views to display.

I see others have had this issue but some how their solutions of calling viewWillAppear directly causes my app to crash. Other solutions were related to Navigation Controller and pushingView's but thats not what i'm using either! What am I missing?

Thanks in advance for your help! :)

See below: View Controller #1 - Currently being displayed on screen

  NSLog(@"FirstViewController - someButtonPressed");
  SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
  secondViewController.myLocation = self.myLocation;
  secondViewController.myDatabase = self.myDatabase;
  [self.view addSubview:secondViewController.view];
  //[secondViewController viewWillAppear:YES];


- (void)viewWillAppear:(BOOL)animated {
  NSLog(@"SecondViewController - viewWillAppear");
  [super viewWillAppear:animated];
  // updating ivars with data

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {   
    NSLog(@"SecondViewController - viewDidLoad");
    [super viewDidLoad];

If I understand it correctly viewDidLoad runs once on the first instance and viewWillAppear runs every time a view is added to the stack of views to display.

-viewDidLoad is called every time a UIViewController's view is loaded. That may be many times during a single controller's life as the view may be unloaded to free up memory when it is not visible and reloaded, triggering another call to -viewDidLoad, when needed.

-viewWillAppear: is called when a UIViewController's view becomes visible. However UIKit assumes that UIViewController's views will fill their window. Nesting UIViewControllers' views is an example of abusing UIViewControllers and will result in unexpected behavior. As you have seen.

See About Custom View Controllers in the View Controller Programming Guide for iOS:

Each custom view controller object you create is responsible for managing all of the views in a single view hierarchy. In iPhone applications, the views in a view hierarchy traditionally cover the entire screen, but in iPad applications they may cover only a portion of the screen. The one-to-one correspondence between a view controller and the views in its view hierarchy is the key design consideration. You should not use multiple custom view controllers to manage different portions of the same view hierarchy. Similarly, you should not use a single custom view controller object to manage multiple screens worth of content.

UIViewController viewWillAppear not called when adding as subView, You should add statisticsController as a child view controller of the controller whose view you're adding it to. self.statisticsController� I'm trying to do some macOS (10.12) programming, and what works in iOS doesn't seem to work in this world. I have an NSViewController subclass, Desk. It's viewDidLoad() method is being called, but not viewWillAppear(). The view does appear, however. I am instantiating Desk from storyboard, but I don't know why that would cause a problem.

If you wrote a custom UIViewController Container you might have overwritten the following method, which leads to your described behavior.

- (BOOL)shouldAutomaticallyForwardAppearanceMethods{
    return NO;

In this case you have to manually handle beginAppearanceTransition/endAppearanceTransition. See Apples View Controller Containment article

Why isn't viewWillAppear getting called? | by Sean Berry, However, you'll get into trouble when you start presenting views over your current screen, “Do not use this style to present a view controller.”. Using custom transitions; At the same time, I want to make sure that when it is dismissed, the view controller behind it is aware of being pushed to the foreground. My basic idea is to try some different configurations and see which one will lead to viewWillAppear being called on the view controller behind. Attempt 1

viewWillAppear: is called when a view controller is displayed in one of the normal ways (e.g. by selecting a tab in a UITabBarController, by pushing onto a UINavigationController, by being popped back to in a UINavigationController, by being presented with presentModalViewController:animated, by being uncovered after dismissModalViewControllerAnimated:, etc). Just displaying a view with addSubview: does not call the method.

It is possible to correctly call viewWillAppear: manually, but in general it's better to use one of the normal ways mentioned above.

iPhone viewWillAppear not firing, about people having problems with viewWillAppear when you do not create your view hierarchy just right. If I create a RootViewController and call addSubView on that controller, I would expect the added I've run into this same problem. How Does this differ from calling the delegate methods manually when a viewcontroller’s view is a subview of another VC? Honestly this seems like a tremendous waste of time. Custom implementations of ViewDidLoad, viewDidLoad, viewDidUnload, viewWillAppear, viewWillDisappear not to mention things as simple as properties like, say, “view”…

Just try this.. I got it working :)

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated]; 
  NSLog(@"SecondViewController - viewWillAppear");

  // updating ivars with data

viewWillAppear not called in subordinate view, viewWillAppear not called in subordinate view - ios. I do see my view (I've changed its background colour) it's only the initialisation (viewWillAppear) that does not run. Does resizing not work when you add it with addSubview method ? About v1.2.8's demo projects, the first child viewController's viewWillAppear does get called, so it sounds weird If the version is 1.2.8 and the implementation looks properly, it's very discouraged idea but you can force call viewWillAppear like:

When you push view or present a view controller by pushViewController:animated or presentModelViewController:animated:, they will call viewWillAppear:animated:, and else method for you. But if you addSubview: manually, you need to call those method by self.

Kill your viewDidLoad configuration in Swift, Using some tricks in Swift, we can provide clear, readable initalization outside of the parts of our view that we want to modify before placing it as a subview. these as a way to provide property configuration but doesn't give them a specific name, the property lazy and self will exist when your initialization closure is run . You can override this method to perform custom tasks associated with displaying the view. For example, you might use this method to change the orientation or style of the status bar to coordinate with the orientation or style of the view being presented. If you override this method, you must call super at some point in your implementation.

iOS, My UIView is created via Storyboard and I know that there is not way to create This is fine } Only when I try to add it as a subview. override func viewWillAppear (_ this issue in iOS 13.5.1 running in a iPhone 7 and My Xcode version is 11.5. Hi all, Clicking on the Tel: links does not open the phone dialer with the number from the link like they would if used used in Chrome/Safari. This site uses cookies for analytics, personalized content and ads.

Unexpected frame and bounds sizes, But when I print the subview.bounds and subview.frame to the console, I get (0, 0, 240, 128) this happens on both the iPhone 5 and iPhone 6 simulators, with have different widths. Frames do not have valid values until after layout has been performed, i.e. I was doing it first in viewDidLoad, and then in viewWillAppear. viewWillAppear(_ animated: Bool) This is called right before your view actually displays to the user. It’s a good time to do any setup for the view that needs to be run everytime you show this view. Note that this is called everytime the view is about to be shown on screen, and the code is run before the user is shown the view.

Child View Controllers, Just like how a UIView can be added to another UIView to form a addSubview( child.view) // Then, add the child to the parent parent. we start using child view controllers extensively in a project, things can get repetitive quite quickly. and viewWillAppear , even when used as a child, which can be really� Re: What should I do if my dismiss does not work after a QRcode scan? Level 8 (8,835 points) Claude31 Oct 29, 2019 6:01 AM ( in response to beginerIOS )

  • Thanks Jonah. I think I understand what's going on. Using addSubView is more for adding smaller view's to a VC e.g. UILabel, UIImage, a custom UIView, all things that will compose the current VC's view. It's not meant for stacking a complete view of VC#2 on top of current VC#1. For that, I need to use NavControllers, TabControllers or MyCustomControllers. Using addSubView works in appDelegate becoz the appDelegate & MainWindow has only added a UIWindow to the screen. To get a view, u need to add a VC with an associated view and since that's the only view stacked on the display, it works fine.
  • That's right. Each of those sub views could still have controller objects responsible for managing them. Their controllers should just not be UIViewControllers. For example you might have a UITableView as a subview and a controller which acts as its delegate and datasource. That way your avoid having a single monolithic controller class.
  • I'm certainly guilty for adding the views of UIViewController subclasses to the views of other UIViewController subclasses. I for one thought this was more or less the whole idea of the UIKit MVC model...
  • I've split off my questions regarding this topic to another thread:…
  • @ScottAllen thanks but please be aware that is answer is fairly old. In iOS 5 and it is valid to create container view controllers whose children manage a small part of the parent view. You still need to work with the framework and use the appropriate add/remove child view controller methods in order for UIViewController methods like -viewWillAppear: to be called when you expect but at least you have some more options for nesting view controllers now.
  • HeikoG, this was not the case. I was using the standard template UIViewControllers generated by Xcode. This question was during iOS4 so things may have changed now.
  • Nope.. it didn't work for me. I think @Jonah answer holds the key.
  • ModalVC is not for navigating through VC's according to Apple's guidelines. PushVC is associated with NavController and that's something I didn't want to use but after researching @Jonah's comment, it's what I can use to solve the issue. I did try calling viewWillAppear:animated manually and the app crashes so that doesn't work.