UIView显示延迟如果使用自动布局以编程方式创建

时间:2016-04-07 13:56:00

标签: ios objective-c uiview autolayout nslayoutconstraint

我被困住了!我正在研究一个viewController,它将成为其他viewControllers的基础。其他人会从中延伸出来。

我想为baseViewController创建一些带有编程约束的视图(如自定义NavigationBar)并创建它。

所以我有故事板,有一个带有rootViewController(HomeViewController)的navigationController,这个homeViewController从baseViewController扩展。

这是我的故事板看起来像;

Storyboard scene

所以,我的约束正在发挥作用!但是,以编程方式约束创建的视图会在几秒后显示出来并且让我疯狂!

以下是应用首次运行时的外观;

enter image description here

几秒钟后,我的观点出现了;

enter image description here

我想分享我的代码。我使用可视格式语言来创建约束;

- (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];

}

我有很多问题;

  • 可能是什么原因?
  • 我可以在需要时使用故事板 创建编程约束?
  • 我正在使用viewDidLoad来调用创建方法 查看和设置约束。 viewDidLoad是个好地方吗?
  • 此方案的最佳做法是什么?
  • 如何像第一张图片一样立即初始化我的视图?

我希望这很清楚。 感谢您的回答,我期待着您的回复。

2 个答案:

答案 0 :(得分:0)

首先,如果不是强制性的,则从IB设置约束。如果必须,则在viewDidAppear和主线程上执行您的任务(如果需要)。

答案 1 :(得分:0)

在设置约束后立即调用setNeedsLayoutlayoutIfNeeded方法。它会对你有用。