在我的 View Controller 中,我有两个对象。 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 中滚动时移动顶 View ,所以我创建了这个函数:
- (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]|)
如何解决这个问题?
最佳答案
将此约束保存在 View Controller 中的某处
// 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];
然后通过保存的约束更改顶 View 的 y 位置:
_topViewConstraint.constant = yOffset;
其中 yOffset 是您自定义方法中的计算值。
哦,你会想要动画这个变化:
[UIView animateWithDuration:0.3 animations:^{
[_topView layoutIfNeeded];
}];
关于ios - 在自动布局环境中移动 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21524394/