gpt4 book ai didi

ios - viewDidDisappear 时布局被破坏

转载 作者:行者123 更新时间:2023-11-29 03:22:19 27 4
gpt4 key购买 nike

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

enter image description here

我有一个带有 UIScrollView 和一些 subview 的 viewController,我通过界面生成器添加其中一些,通过代码添加其他一些。当我调用推送下一个 viewController 的方法时,当前 View 会中断,并且当我弹出它时仍然会中断。

我尝试从 Storyboard 中删除自动布局设置,但仍然不起作用。

我以编程方式设置 View 的框架,并移动 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;
}

有什么想法吗?谢谢!

最佳答案

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

当您想执行一些动画来更改屏幕上所有内容的位置时,请使用 UIView 动画 block 并仅修改约束中的常量

如果你想重置,再次修改约束上的常量

关于ios - viewDidDisappear 时布局被破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20920555/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com