繁体   English   中英

如果使用自动布局以编程方式创建UIView,则显示晚

[英]UIView Display Late If Creating It Programmatically with Auto Layout

我被卡住了! 我正在研究一个viewController,它将成为其他viewController的基础。 其他人将从中扩展。

我想以编程方式为baseViewController创建一些视图(如自定义NavigationBar),并创建了它。

所以我有了情节提要,有一个带有rootViewController(HomeViewController)的navigationController,而这个homeViewController是baseViewController的扩展。

这是我的故事板样子;

故事板场景

因此,我的约束正在起作用! 但是通过编程约束创建的视图会在几秒钟后显示,这让我发疯!

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

在此处输入图片说明

几秒钟后,我的视线出现了。

在此处输入图片说明

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

- (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的好地方吗?
  • 这种情况下的最佳做法是什么?
  • 如何像第一张照片一样立即初始化视图?

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

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

在约束设置之后立即调用setNeedsLayoutlayoutIfNeeded方法。 它将为您工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM