UIView Display Late If Creating It Programmatically with Auto Layout
<p>I'm stuck! I'm working on a viewController which is going to be a base for other viewControllers. Others will extend from it. </p>

<p>I want to create some view (like custom NavigationBar) with <strong>programmatically constraints</strong> for baseViewController and I created it. </p>

<p>So I have storyboard, there is a navigationController with a rootViewController (HomeViewController) and this homeViewController extend from baseViewController.</p>

<p>Here is my storyboard looks like;</p>

<p><a href="https://i.stack.imgur.com/a70Qj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/a70Qj.png" alt="Storyboard scene"></a></p>

<p>So, my constraints are working! But the view which is created with programmatically constraints display after couple of second and its drive me crazy!</p>

<p>Here is how it looks when app first running;</p>

<p><a href="https://i.stack.imgur.com/HFj75.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HFj75.png" alt="enter image description here"></a></p>

<p>And after couple of seconds my view appears;</p>

<p><a href="https://i.stack.imgur.com/X4KC0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/X4KC0.png" alt="enter image description here"></a></p>

<p>I want to also share my codes. I am using visual format language to create constraints;</p>

<pre><code>- (void)viewDidLoad {
[super viewDidLoad];
[self initializeBaseView];
// Do any additional setup after loading the view.}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Initialize BaseView

- (void)initializeBaseView{

    //Background View
    if (self.backgroundContainerView == nil) {
        self.backgroundContainerView = [UIView new];
        self.backgroundContainerView.translatesAutoresizingMaskIntoConstraints = NO;
        [self.view addSubview:self.backgroundContainerView];
        [self.view sendSubviewToBack:self.backgroundContainerView];
    }
    [self initConstraint_1];


    //Navigation Header View
    if(self.navigationBarContainerView == nil){
        self.navigationBarContainerView = [UIView new];
        self.navigationBarContainerView.translatesAutoresizingMaskIntoConstraints = NO;
        [self.navigationBarContainerView setBackgroundColor:[UIColor clearColor]];
        [self.view addSubview:self.navigationBarContainerView];

    }
    [self initConstraint_2];

    if (self.backgroundType==BackgroundTypeWhite) {
        //very light Gray
        [self.backgroundContainerView setBackgroundColor:APP_GRAY_COLOR_ATHENS];
    }

    [self initializeBaseStyle];
}

- (void)initializeBaseStyle{
    [self.navigationBarContainerView setBackgroundColor:[UIColor darkGrayColor]];
}



#pragma mark - Initialize Constraints

- (void)initConstraint_1{
    // 1. Create a dictionary of views
    NSDictionary *viewsDictionary = @{@"firstView":self.backgroundContainerView};
    NSDictionary *metrics = @{@"vSpacing":@0, @"hSpacing":@0};

    // 2. Define the view Position and automatically the Size
    NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-vSpacing-[firstView]-hSpacing-|"
                                                                        options:0
                                                                        metrics:metrics
                                                                          views:viewsDictionary];

    NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hSpacing-[firstView]-hSpacing-|"
                                                                        options:0
                                                                        metrics:metrics
                                                                          views:viewsDictionary];

    [self.view addConstraints:constraint_POS_V];
    [self.view addConstraints:constraint_POS_H];

}


- (void)initConstraint_2{
    NSDictionary *viewsDictionary = @{@"firstView": self.navigationBarContainerView, @"secondView": self.logoImage};
    NSDictionary *metrics = @{@"vSpacing":@74,
                              @"hSpacing":@0,
                              @"BottomSpacing":@60,
                              @"firstViewHeight":@64
                              };


    // 2. Define the view Position and automatically the Size (for the firstView)
    NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[firstView(firstViewHeight)]"
                                                                        options:0
                                                                        metrics:metrics
                                                                          views:viewsDictionary];

    NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hSpacing-[firstView]-hSpacing-|"
                                                                        options:0
                                                                        metrics:metrics
                                                                          views:viewsDictionary];

    [self.view addConstraints:constraint_POS_V];
    [self.view addConstraints:constraint_POS_H];

}
</code></pre>

<p>I have plenty of questions;</p>

<ul>
<li>What is the reason could it be? </li>
<li>Can I use storyboard when I want to
create programmatically constraints? </li>
<li>I'm using viewDidLoad to call methods for creating
view and set constraints. Is viewDidLoad good place for that?</li>
<li>What is the best practice for this scenario?</li>
<li>How I can manage to initialize my view immediately as like as first picture?</li>
</ul>

<p>I hope that's clear.
Thank you for your answers and I'm looking forward to hearing from you. </p>