在我的视图控制器中,我有两个对象。 TopView和CustomScrollView。
我想将TopView放在顶部,并将CustomScrollView放在它们下面,所以我把它放在了这里:
// TOP VIEW LAYOUT
// horizontal
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:| [_topView]|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(_topView, self)]];
// vertical
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_topView(89)]" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(_topView, self)]];
// CUSTOMSCROLLVIEW
// horizontal
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_customScrollVeiw]|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(_customScrollVeiw, self)]];
// vertical
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_topView]-(0)-[_customScrollVeiw]|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(_customScrollVeiw, _topView, self)]];
并且工作正常。
但是现在我想在CustomScrollView中滚动时移动Top视图,所以我创建了这个函数:
- (void) moveOnScroll: (CGPoint) offset contentSize: (CGSize) contentSize
{
float delta = offset.y - self.yPosition;
if (self.yPosition > offset.y) {
if (labs(self.frame.origin.y) < self.frame.size.height/50 ){
self.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}
else if (self.frame.origin.y - delta < 0 && offset.y < (contentSize.height - self.screenHeight)){
self.frame = CGRectMake(0, self.frame.origin.y - delta, self.frame.size.width, self.frame.size.height);
}
else{
self.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}
}
else {
if (self.frame.origin.y > (- self.frame.size.height + self.labelHeight) && offset.y > 0){
if (self.frame.origin.y - delta < (- self.frame.size.height + self.labelHeight )) {
self.frame = CGRectMake(0, (- self.frame.size.height + self.labelHeight ), self.frame.size.width, self.frame.size.height);
}
else{
self.frame = CGRectMake(0, self.frame.origin.y - delta , self.frame.size.width, self.frame.size.height);
}
}
}
self.yPosition = offset.y;
}
现在,当我滚动时,TopView隐藏了,但是CustomScrollView不再与TopView拥抱,它停留在他的位置并且不移动。 (V:[_ topView]-(0)-[_ customScrollVeiw] |)
如何解决这个问题?
最佳答案
将此约束保存在视图控制器中的某个位置
// vertical
NSArray *topViewContstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_topView(89)]" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(_topView, self)]
for (NSLayoutConstraint *constraint in topViewContstraints) {
if (constraint.firstAttribute == NSLayoutAttributeHeight) {
_topViewConstraint = constraint;
break;
}
}
[self.view addConstraints:topViewContstraints];
然后通过保存的约束更改顶视图的y位置:
_topViewConstraint.constant = yOffset;
其中yOffset是您的自定义方法中的计算值。
哦,您将希望对此更改进行动画处理:
[UIView animateWithDuration:0.3 animations:^{
[_topView layoutIfNeeded];
}];
关于ios - 在自动版面环境中移动UIView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21524394/