viewDidDis出现时布局中断

时间:2014-01-04 11:47:07

标签: ios iphone ipad autolayout

我几天试图解决以下问题,但我没有得到任何结果:(

enter image description here

我有一个带有UIScrollView和一些子视图的viewController,我正在通过接口构建器添加其中一些,而其他代码则通过代码添加。当我调用方法来推送下一个viewController时,当我弹出它时,当前视图会中断并且仍然会中断。

我尝试从故事板中删除Autolayout设置但仍无效。

我正在以编程方式设置视图的帧,我移动scrollView的内容偏移量来制作动画。

我在viewWillAppear中调用此方法:

- (void)prepareQuestion
{
    int nextYposition;

    // Question
    NSString *q = [self.theQuestion getQuestionTextForLanguage:lang];
    NSString *badText = [self.theQuestion getInfTextForLanguage:lang];
    NSString *goodText = [self.theQuestion getSupTextForLanguage:lang];

    self.answer.questionID = self.theQuestion.questionID;

    if (whiteStyle) {
        question = [[NPSQuestionViewController alloc] initWithQuestion:q AndStyle:kNPSQuestionStyleWhite];
    } else {
        question = [[NPSQuestionViewController alloc] initWithQuestion:q AndStyle:kNPSQuestionStyleOther];
    }

    question.view.center = CGPointMake(self.view.center.x, self.view.center.y - 50);
    [self addChildViewController:question];
    [self.scrollView addSubview:question.view];

    nextYposition = question.view.frame.origin.y + question.view.frame.size.height;

    int questionHeight = 0; // Guardamos el alto dependiendo del tipo de control

    if (self.sliderStyle) {
        // Slider test
        if (whiteStyle) {
            NPSSliderViewControl = [[NPSSliderViewController alloc] initWithStyle:kNPSSliderViewStyleWhite];
        } else {
            NPSSliderViewControl = [[NPSSliderViewController alloc] initWithStyle:kNPSSliderViewStyleOther];
        }

        NPSSliderViewControl.iconSet = [[[OpinatManager sharedInstance] campanya] tipoIconos];
        [NPSSliderViewControl setBadText:badText andGoodText:goodText];
        [self addChildViewController:NPSSliderViewControl];
        [self.scrollView addSubview:NPSSliderViewControl.view];
        NPSSliderViewControl.delegate = self;

        CGRect frame = NPSSliderViewControl.view.frame;
        [NPSSliderViewControl.view setFrame:CGRectMake(0, nextYposition, frame.size.width, frame.size.height)];
        questionHeight = frame.size.height - 33; // 33 es el valor de la parte que desaparece del control

    } else {
        // Control
        if (whiteStyle) {
            NPSBtnViewControl = [[NPSBtnViewController alloc] initWithStyle:kNPSBtnViewStyleWhite];
        } else {
             NPSBtnViewControl = [[NPSBtnViewController alloc] initWithStyle:kNPSBtnViewStyleOther];
        }

        NPSBtnViewControl.iconSet = [[[OpinatManager sharedInstance] campanya] tipoIconos];
        [NPSBtnViewControl setBadText:badText andGoodText:goodText];
        [self addChildViewController:NPSBtnViewControl];
        [self.scrollView addSubview:NPSBtnViewControl.view];
        NPSBtnViewControl.delegate = self;

        CGRect frame = NPSBtnViewControl.view.frame;
        [NPSBtnViewControl.view setFrame:CGRectMake(0, nextYposition, frame.size.width, frame.size.height)];
        questionHeight = frame.size.height - 58; // 60 es el valor de la parte que desaparece del control
    }

    nextYposition += questionHeight;

    // Motives question
    if (whiteStyle) {
        motivesQuestion = [[NPSQuestionViewController alloc] initWithQuestion:self.insatisfactionTitle AndStyle:kNPSQuestionStyleWhite];
    } else {
        motivesQuestion = [[NPSQuestionViewController alloc] initWithQuestion:self.insatisfactionTitle AndStyle:kNPSQuestionStyleOther];
    }

    CGRect secondQuestionFrame = motivesQuestion.view.frame;
    secondQuestionFrame.origin.y = nextYposition;
    motivesQuestion.view.frame = secondQuestionFrame;
    motivesQuestion.view.alpha = 0;
    [self.scrollView addSubview:motivesQuestion.view];
    motivesQuestion.view.tag = 22;

    questionHeight = motivesQuestion.view.frame.size.height;

    nextYposition += questionHeight;

    // Setting up table Y position. Only works if I setted in viewDidAppear
    nextPosition = nextYposition;
    resetPosition = nextYposition;

    // Ajustamos la tableView
    self.tableMotives.alpha = 0;
}

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:1)

如果你正在使用自动布局,你基本上不应该直接设置视图框。相反,您应该设置约束,以便将所有内容自动放置在正确的位置,并使用@property来保存对所选约束的“特殊”引用。这些“特殊”约束是控制屏幕上其他内容整体位置的约束,它们通过constant offset来工作。

如果要执行某些动画以更改屏幕上所有内容的位置,请使用UIView动画块并仅修改约束上的constant

如果要重置,请再次修改约束的constant