使用viewDidLayoutSubviews排列视图控制器的控件时,我会遇到奇怪的行为。我在界面构建器中的视图控制器上放置了多个控件(标签,文本框,日期选择器和分段控件)。在viewDidLoad中,我隐藏了其中一些控件,其余的需要重新排列(放大),以便隐藏控件没有间隙。
加载视图时,会触发viewDidLayoutSubviews,并根据需要排列控件。但是如果你点击分段控制它就会失去"它的框架起源并从IB恢复到其原始位置。什么是射击会使分段控制失去其帧起源?
然后,如果您编辑文本字段,viewDidLayoutSubviews将再次触发,分段控件将移回所需位置。这似乎不合适 - 我没有执行任何需要此操作的setFrame或其他操作。
- (void)viewDidLoad {
[super viewDidLoad];
// arbitrarily hide two of the controls
_textField2.hidden = YES;
_textField4.hidden = YES;
}
- (void)viewDidLayoutSubviews {
// move the controls up in the view if any preceding controls are hidden
float currentYPosition = _textField1.frame.origin.y;
NSArray *arrayControls = @[_textField1, _textField2, _segmentedControl1, _textField3, _textField4, _textField5];
for (int i=0; i < arrayControls.count; i++) {
// move the associated control...also capture the offset to the next label/control
UIView <NSObject> *object = (UIView <NSObject> *)[arrayControls objectAtIndex:i];
float newYPosition = [self rearrangeFrame:object withCurrentYPosition:currentYPosition];
// update our reference to the current Y position
currentYPosition = newYPosition;
}
}
- (float)rearrangeFrame:(id)controlObject withCurrentYPosition:(float)topSpaceToSuperview {
// input: control on the view that needs to be rearranged
// output: the updated y coordinate for the next control
float padding = 3.0f;
if ([controlObject conformsToProtocol:@protocol(NSObject)]) {
UIView <NSObject> *object = (UIView <NSObject> *) controlObject;
if (object.hidden) {
// return a box with a height of 0 if the control is hidden
[object setFrame:CGRectMake(object.frame.origin.x, topSpaceToSuperview, object.frame.size.width, 0)];
return topSpaceToSuperview;
} else {
// update the y position of the control's frame origin
[object setFrame:CGRectMake(object.frame.origin.x, topSpaceToSuperview, object.frame.size.width, object.frame.size.height)];
return topSpaceToSuperview + object.frame.size.height + padding;
}
} else {
return topSpaceToSuperview;
}
}
答案 0 :(得分:0)
这可能是因为自动布局已打开(默认情况下处于启用状态)。使用自动布局移动视图时,您必须更改约束,而不是设置框架。所以要解决这个问题。您需要关闭自动布局或更改代码以使用约束。